Hi all,
So I have implemented the same code in both C++ and Python. The code does nothing much, just reads a grayscale image, implements the hough circle detection algorithm and gives the output.
Code in C++:
#include <iostream>
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/opencv.hpp"
#define IMAGE_ROW 1882
int t_rows, t_cols;
using namespace cv;
int main()
{
Mat img = imread("<path>\\org5_6.bmp", 0);
int circle[3];
std::cout << "OpenCV Version = " << CV_VERSION << '\n';
if (!img.data)
{
std::cout << "Could not load image." << '\n';
}
else
{
std::cout << "Start HCircles.. " << '\n';
t_rows = img.rows;
t_cols = img.cols;
std::cout << "t_cols : " << img.cols << std::endl;
std::cout << "t_rows : " << img.rows << std::endl;
std::vector<cv::Vec3f> circles;
int circlesExist = 1;
Mat GrayImg = img;
Mat ResizedImg;
Mat BlurredImg;
int black_circle_radius_low;
int black_circle_radius_upper;
//cvtColor(RGBimg, GrayImg, CV_BGR2GRAY);
//GrayImg = imread("/Volumes/Seagate Backup Plus Drive/CppTest/org5_6.bmp", 0);
Size size(t_cols * IMAGE_ROW / t_rows, IMAGE_ROW);
resize(GrayImg, ResizedImg, size);
std::cout << "After resizing" << '\n';
std::cout << "t_cols : " << ResizedImg.cols << std::endl;
std::cout << "t_rows : " << ResizedImg.rows << std::endl;
t_rows = ResizedImg.rows;
t_cols = ResizedImg.cols;
medianBlur(ResizedImg, BlurredImg, 5);
Mat BlurredImgCopy = BlurredImg;
black_circle_radius_low = (int)(0.4 * std::min(t_rows, t_cols));
black_circle_radius_upper = (int)(0.45 * std::min(t_rows, t_cols));
std::cout << "Radius min = " << black_circle_radius_low << '\n';
std::cout << "Radius max = " << black_circle_radius_upper << '\n';
HoughCircles(BlurredImg, circles, CV_HOUGH_GRADIENT, 1, 300, 30, 30, black_circle_radius_low, black_circle_radius_upper);
std::cout << "Total number of circles detected = " << circles.size() << std::endl;
if (circles.size() == 0)
{
std::cout << "No circles were detected" << '\n';
circlesExist = 0;
circle[0] = 0;
circle[1] = 0;
circle[2] = 0;
}
else
{
for (size_t i = 0; i < circles.size(); i++)
{
//Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
//int radius = cvRound(circles[i][2]);
std::cout << "Circle Center - (x: " << circles[i][0] << " y: " << circles[i][1] << ") and Radius: " << circles[i][2] << '\n';
/*
// circle center
circle( BlurredImgCopy, center, 3, Scalar(0,255,0), -1, 8, 0 );
// circle outline
circle( BlurredImgCopy, center, radius, Scalar(0,0,255), 100, 8, 0 ); */
}
}
}
}
And for Python:
import cv2
import cv2.cv as cv
import numpy
IMAGE_ROW = 1882
print "OpenCV version = ",cv2.__version__
FILENAME = '<path>/org5_6.bmp'
img = cv2.imread(FILENAME, 0)
print "Read image"
#cv2.imshow('img', img)
#cv2.waitKey(0)
circle_exsit=1
[t_rows,t_cols]= img.shape
print 't_rows = ', t_rows
print 't_cols = ', t_cols
img=cv2.resize(img, (t_cols*IMAGE_ROW/t_rows,IMAGE_ROW))
img = cv2.medianBlur(img, 5)
[t_rows,t_cols]=img.shape
print 't_rows = ', t_rows
print 't_cols = ', t_cols
limit=min(t_rows,t_cols)
black_circle_radius_low=int(0.4*limit)
black_circle_radius_upper=int(0.45*limit)
print 'r_min = ',black_circle_radius_low
print 'rmax = ',black_circle_radius_upper
circles = cv2.HoughCircles(img,cv.CV_HOUGH_GRADIENT,1,300,param1=30,param2=30,minRadius=black_circle_radius_low,maxRadius=black_circle_radius_upper)
print circles
These codes are quite similar, but the results are different for C++ and Python -
Output from C++:
OpenCV Version = 2.4.11
Start HCircles..
t_cols : 3664
t_rows : 2748
After resizing
t_cols : 2509
t_rows : 1882
Radius min = 752
Radius max = 846
Total number of circles detected = 3
Circle Center - (x: 1325.5 y: 1094.5) and Radius: 759.537
Circle Center - (x: 877.5 y: 1077.5) and Radius: 790.734
Circle Center - (x: 896.5 y: 512.5) and Radius: 806.832
From Python:
OpenCV version = 2.4.11
Read image
t_rows = 2748
t_cols = 3664
t_rows = 1882
t_cols = 2509
r_min = 752
rmax = 846
[[[ 1326.5 1075.5 752.53338623]
[ 896.5 512.5 806.83239746]
[ 877.5 1077.5 790.73413086]]]
Anyone faced the same problem?
I asked the question on StackOverflow before, but that time, I was using different machines and different python versions. Now I am running the code on the same machine and as you can see from the outputs, the versions are the same.
Cheers.