csdnyiqixuexiba 2016-01-18 12:57 采纳率: 33.3%
浏览 3435

opencv连续读取多张图片,并对每张图片进行霍夫圆操作后,只能读取第一张图片

当单独对第一个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;

}

  • 写回答

2条回答 默认 最新

  • 91program 博客专家认证 2016-01-18 13:04
    关注

    加上霍夫变换后出现的问题,说明你的霍夫变换部分的代码有问题。
    可能存在内存访问越界的情况,建议你调试一下代码、分析一下执行过程中是不是出现异常的情况。

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题