OpenCV中文网站

 找回密码
 立即注册
搜索
热搜: 安装 配置
查看: 6871|回复: 9

有關於尋找圓的中心點問題

[复制链接]
发表于 2015-5-31 15:48:43 | 显示全部楼层 |阅读模式
本帖最后由 bob21111 于 2015-5-31 18:46 编辑

目前知道使用 HoughCircles 可以找到圓的中心點只是有時換張圖片,之前設定好的參數就不能正確地找到圓心
用HoughCircles 找的圓心,感覺不是很正確
hough.bmp
那還有其他方法可以找中心點嗎?
canny和sobel邊緣檢測 可以尋找圓的中心點嗎
目前對於opencv和emgucv還有很多不懂
請問有人可以幫忙解答嗎? 純粹用emgucv能做到嗎?
原圖
001_1_1.bmp





回复

使用道具 举报

发表于 2015-5-31 18:08:23 | 显示全部楼层
大哥,要原图,处理的图不好弄了。
回复 支持 1 反对 0

使用道具 举报

发表于 2015-5-31 17:06:05 | 显示全部楼层
cvFitEllipse(PointArray2D32f, count, box);/********Fitting current contour********/,可以试一下这个函数进行拟合,详细可参考我之前的回复http://www.opencv.org.cn/forum.p ... id=35669&extra=。你最好是上图,这样才能更好的回复你。
               
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-5-31 17:25:19 | 显示全部楼层
Davidgao 发表于 2015-5-31 17:06
cvFitEllipse(PointArray2D32f, count, box);/********Fitting current contour********/,可以试一下这个 ...

圖片我重新用上了
目前想要利用HoughCircles 找到瞳孔的中心點
但有時都歪歪的,所以想看有沒有其他的方法可以使用

感謝你的回覆,我會研究看看
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-5-31 18:47:19 | 显示全部楼层
Davidgao 发表于 2015-5-31 18:08
大哥,要原图,处理的图不好弄了。

哈哈 抱歉啦  
重新又丟了一張
回复 支持 反对

使用道具 举报

发表于 2015-5-31 21:11:03 | 显示全部楼层

#include "time.h"
#include <stdio.h>

#include <cv.h>
#include <cxcore.h>
#include <highgui.h>

/*
  src and dst are grayscale, 8-bit images;
  Default input value:
           [low, high] = [0,1];  X-Direction
           [bottom, top] = [0,1]; Y-Direction
           gamma ;
  if adjust successfully, return 0, otherwise, return non-zero.
*/
int ImageAdjust(IplImage* src, IplImage* dst,
            double low, double high,   // X方向:low and high are the intensities of src
            double bottom, double top, // Y方向:mapped to bottom and top of dst
            double gamma )
{
        if(         low<0 && low>1 && high <0 && high>1&&
                bottom<0 && bottom>1 && top<0 && top>1 && low>high)
        return -1;
    double low2 = low*255;
    double high2 = high*255;
    double bottom2 = bottom*255;
    double top2 = top*255;
    double err_in = high2 - low2;
    double err_out = top2 - bottom2;

    int x,y;
    double val;

    // intensity transform
    for( y = 0; y < src->height; y++)
    {
        for (x = 0; x < src->width; x++)
        {
            val = ((uchar*)(src->imageData + src->widthStep*y))[x];
            val = pow((val - low2)/err_in, gamma) * err_out + bottom2;
            if(val>255) val=255; if(val<0) val=0; // Make sure src is in the range [low,high]
            ((uchar*)(dst->imageData + dst->widthStep*y))[x] = (uchar) val;
        }
    }
    return 0;
}


int main(int argc, char* argv[])
{
        IplImage* img_src = NULL;
        IplImage* img_show = NULL;
        IplImage* img_gray = NULL;
    IplImage* img_edge = NULL;
        IplImage* img_dst = NULL;
        IplImage* img_temp = NULL;

        CvPoint p_center;
    CvSize size;

        CvMemStorage* storage = cvCreateMemStorage(0);
        CvSeq* seq = 0;  

        int i, j, k;
        LONGLONG persecond, t1, t2;
        double t_diff = 0, time = 0;

        /********Parameter for circle detection********/
        CvSeq* ellipses;
    CvBox2D32f* box;
    CvPoint* PointArray;
    CvPoint2D32f* PointArray2D32f;
    CvPoint center;
        int count = 0, n_ellipse = 0;
        double ratio = 0, perim = 0;
        double find_x = 0, find_y = 0;
        double max_h = 0, min_h = 1000;
        int cur_x = 0, cur_y = 0;
    int pre_x = 0, pre_y = 0;
        double max_scale = 0, min_scale = 0;
        double dist = 0;
       
        cvNamedWindow("Raw", 1);
        cvNamedWindow("Interim",1);  
        cvNamedWindow("Result",1);
  
        cvMoveWindow("Raw", 30, 0);
        cvMoveWindow("Interim", 360, 0);
        cvMoveWindow("Result", 690, 0);

        img_src = cvLoadImage("iris.bmp",1);
        img_show = cvCreateImage(cvSize(img_src->width, img_src->height),IPL_DEPTH_8U,3);
        img_gray = cvCreateImage(cvSize(img_src->width, img_src->height),IPL_DEPTH_8U,1);
        img_edge = cvCreateImage(cvSize(img_src->width, img_src->height),IPL_DEPTH_8U,1);
        img_temp = cvCreateImage(cvSize(img_src->width, img_src->height),IPL_DEPTH_8U,3);
        img_dst = cvCreateImage(cvSize(img_src->width, img_src->height),IPL_DEPTH_8U,1);

        p_center = cvPoint(cvRound(img_src->width/2), cvRound(img_src->height/2));

        QueryPerformanceFrequency((LARGE_INTEGER *)&persecond);
        QueryPerformanceCounter((LARGE_INTEGER *)&t1);

        /************************Part1:************************/
        /********1.0:Convert to gray channel********/
        cvCvtColor(img_src, img_gray, CV_BGR2GRAY);

        /********1.1:Contrast enhancement********/
        ImageAdjust( img_gray, img_edge, 0, 0.5, 0.5, 1, 1);

        /********1.2:Thresholding********/
        cvThreshold(img_edge, img_edge, 0, 255, CV_THRESH_BINARY|CV_THRESH_OTSU);
        cvCvtColor(img_edge, img_temp, CV_GRAY2BGR);

        /********2.5:Ellipse detection********/
        n_ellipse = 0, max_h = 0, min_h = 1000;

        cvCopyImage(img_src, img_show);
        img_show->origin = img_src->origin;
        ellipses = cvCreateSeq(CV_SEQ_ELTYPE_POINT, sizeof(CvSeq), sizeof(CvPoint), storage);
        cvFindContours(img_edge, storage, &ellipses, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_NONE, cvPoint(0,0));

        for(;ellipses;ellipses = ellipses->h_next)
    {
                /*******Min required points for fitting*******/
                count = ellipses->total;
                if(count < 100)           
                        continue;

                /*******Allocate memory for point sets*******/
                PointArray = (CvPoint*)malloc(count*sizeof(CvPoint));
                PointArray2D32f= (CvPoint2D32f*)malloc(count*sizeof(CvPoint2D32f));
                box = (CvBox2D32f*)malloc(sizeof(CvBox2D32f));
                cvCvtSeqToArray(ellipses, PointArray, CV_WHOLE_SEQ);

        /********Convert CvPoint set to CvPoint2D32f********/
                for(i=0; i<count; i++)
        {
            PointArray2D32f[i].x = (float)PointArray[i].x;
            PointArray2D32f[i].y = (float)PointArray[i].y;
        }

                /********Fitting current contour********/
                cvFitEllipse(PointArray2D32f, count, box);

                /********Convert ellipse data from float to integer representation********/
        center.x = cvRound(box->center.x);
        center.y = cvRound(box->center.y);
        size.width = cvRound(box->size.width*0.5);
        size.height = cvRound(box->size.height*0.5);
        box->angle = -box->angle;

                /********Check validity ellipse********/
                if(size.width>size.height)
                {
                        ratio = box->size.height/box->size.width;
                        perim = 2*3.1415*size.height+4*(size.width-size.height);
                }
                else
                {
                        ratio = box->size.width/box->size.height;
                        perim = 2*3.1415*size.width+4*(size.height-size.width);
                }
                if(ratio>0.5&&((int)perim>count))
                {
                        cvEllipse(img_show, center, size, box->angle, 0, 360, CV_RGB(255,0,0), 1, CV_AA, 0);
                }

                /********Free memory********/         
        free(PointArray);
        free(PointArray2D32f);
        free(box);
        }
        cvSaveImage("result.jpg", img_show);
       
        /********Compute time lapse********/       
        QueryPerformanceCounter((LARGE_INTEGER *)&t2);       
        t_diff = t2-t1;       
        time = t_diff/persecond; //(单位:秒)               
        printf("Time lapse:%.6f\n", time);

        /********3:Show image result********/
        img_gray->origin = img_src->origin;
        img_edge->origin = img_src->origin;       
        img_temp->origin = img_src->origin;

        cvShowImage("Raw", img_src);
    cvShowImage("Result", img_show);
    cvShowImage("Interim", img_temp);
        cvWaitKey(0);

        /********Cleanup:********/       
        cvDestroyWindow("Raw");
          cvDestroyWindow("Interim");
          cvDestroyWindow("Result");
          cvReleaseImage(&img_dst);
          cvReleaseImage(&img_temp);
          return 0;
}

result.jpg
回复 支持 反对

使用道具 举报

发表于 2015-5-31 21:14:48 | 显示全部楼层
喔,不少一句画圆心的代码:
cvLine(img_show, center, center, CV_RGB(255, 0, 0), 5, 8);
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-5-31 23:27:00 | 显示全部楼层
Davidgao 发表于 2015-5-31 21:11
#include "time.h"
#include

感謝Davidgao您的幫忙,提供了程式碼
請問一下,目前我使用C#編寫程式
那你所提供的程式碼
能夠在C#上使用嗎?
還是需要用別的方法來使用,或者改寫成C#上的?
回复 支持 反对

使用道具 举报

发表于 2015-6-1 19:53:36 | 显示全部楼层
自己改成C#的吧,语言只是工具。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-6-3 19:35:05 | 显示全部楼层
Davidgao 发表于 2015-6-1 19:53
自己改成C#的吧,语言只是工具。

謝謝你 我試看看
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|OpenCV中文网站

GMT+8, 2024-4-25 05:55 , Processed in 0.011236 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表