所以我将同一个文件夹复制到两台不同的机器上,一台在Python上运行OpenCV,另一台机器在C ++上运行OpenCV。我实际上是将Python代码转换为C ++,我遇到了这个奇异的问题,其中Hough Circle探测器的输出不一样。
C ++的输出:
Radius min = 752
Radius max = 846
Total number of circles detected = 3
Circle - x: 1325.5 y: 1094.5 Radius: 759.537
Circle - x: 877.5 y: 1077.5 Radius: 790.734
Circle - x: 896.5 y: 512.5 Radius: 806.832
Python的输出:
Rmin = 752
Rmax = 846
[[[1326.5 1075.5 752.5333...]
[896.5 512.5 806.8323...]
[877.5 1077.5 790.7341...]]]
C ++来源:
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <string>
#include <iostream>
#include <math.h>
#include <vector>
#include <algorithm>
#define IMAGE_ROW 1882
struct O_HC{
//return types known
int circle[3];
int circlesExist;
}outHC;
int main(string FILENAME)
{
//something
Mat RGBimg = imread(FILENAME, CV_LOAD_IMAGE_COLOR);
//something something
if(! RGBimg.data)
{
std::cout << "Could not load image" << std::endl;
}
else
{
/*
//THIS PIECE OF CODE SHOWS THE IMAGE IN A WINDOW
namedWindow("DisplayWindow", WINDOW_AUTOSIZE);
imshow("DisplayWindow", RGBimg);
waitKey(0);
*/
//Code not involving RGBimg
outHC = houghcircles(RGBimg);
}
return 0;
}
O_HC houghcircles(Mat RGBimg)
{
vector<Vec3f> circles;
cvtColor(RGBimg, GrayImg, CV_BGR2GRAY);
Size size(t_cols * IMAGE_ROW / t_rows, IMAGE_ROW);
resize(GrayImg, ResizedImg, size);
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);
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)
{
outHC.circlesExist = 0;
outHC.circle[0] = 0;
outHC.circle[1] = 0;
outHC.circle[2] = 0;
}
else
{
outHC.circlesExist = 1;
for( size_t i = 0; i < circles.size(); i++ )
{
std::cout << "Circle - x: " << circles[i][0] << " y: " << circles[i][1] << " Radius: " << circles[i][2] << '\n';
//Something else
}
outHC.circle[0] = (int)circles[0][0];
outHC.circle[1] = (int)circles[0][1];
outHC.circle[2] = (int)circles[0][2];
}
return outHC;
}
Python代码:
import cv2
import cv2.cv as cv
IMAGE_ROW = 1882
img = cv2.imread(filename, 0)
[t_rows,t_cols]= img.shape
img = cv2.resize(img, (t_cols*IMAGE_ROW/t_rows,IMAGE_ROW))
img = cv2.medianBlur(img, 5)
[t_rows,t_cols]=img.shape
limit=min(t_rows,t_cols)
black_circle_radius_low=int(0.4*limit)
black_circle_radius_upper=int(0.45*limit)
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
正如我们所看到的,第一个圆圈对于不同的语言是不同的。有没有人知道为什么会这样?
干杯。
PS: OpenCV 2.4.11 随Python一起安装, OpenCV 2.4.13 随C ++一起安装。如果重要,Python版本在Win7机器上运行,C ++版本在OS X Yosemite上运行。我不是那么精通Python,所以在这里和那里可能会出现一些语法错误。
编辑:
添加了一些代码以显示图像读取。