当单独对第一个for循环进行操作时,会显示文件下的4张图片,但是加上霍夫变换后,只能显示一张图片及霍夫变换后的图片,其他的3张图片及变换后的图片都显示不出来,请问这是为什么?
#include "cv.h"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/opencv.hpp"
#include
char filename[100];
char windowname[100];
using namespace cv;
using namespace std;
/** @function main /
int main()
{
for (int j = 1; j <= 4; j++)
{
sprintf(filename, "E:/projects/%d.jpg", j);
sprintf(windowname, "window%d.jpg", j);
IplImage pScr = cvLoadImage(filename, 1);
cvNamedWindow(windowname, CV_WINDOW_AUTOSIZE);
cvShowImage(windowname, pScr);
/// Read the image
Mat src=pScr;//将IplImage类型的图片pScr转换为Mat型的src,因为霍夫变换是对Mat类型进行操作的,不知道这里理解对不对
Mat src_gray;
if (!src.data)
{
return -1;
}
/// Convert it to gray
cvtColor(src, src_gray, CV_BGR2GRAY);
/// Reduce the noise so we avoid false circle detection
GaussianBlur(src_gray, src_gray, Size(9, 9), 2, 2);
vector<Vec3f> circles;
/// Apply the Hough Transform to find the circles
HoughCircles(src_gray, circles, CV_HOUGH_GRADIENT, 1.5, 10, 200, 100, 0, 0);
/// Draw the circles detected
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]);
// circle center
circle(src, center, 3, Scalar(0, 255, 0), -1, 8, 0);
// circle outline
circle(src, center, radius, Scalar(0, 0, 255), 3, 8, 0);
cout << "第" << j << "个图片的直径是:" << 2 * radius << endl;
}
/// Show your results
namedWindow("Hough Circle Transform Demo", CV_WINDOW_AUTOSIZE);
imshow("Hough Circle Transform Demo", src);
waitKey(0);
}
waitKey(0);
return 0;
}