0

我正在研究一個項目,將我的圖像縮小到一個物體的輪廓(其實就是眼睛的瞳孔)。 現在我想對包含瞳孔輪廓的圖像應用霍夫變換,但沒有任何反應。 我的猜測是,你可以將hough變換應用於灰度圖像,如何從輪廓恢復圖像?

所以我想知道如何找回已經檢測到的瞳孔輪廓的灰度部分,以便我可以對其應用hough變換。

+2

霍夫不關心強度,所以你的猜測是錯誤的。其次,二值圖像是灰度圖像,它恰好只有兩個強度。 – mmgp 2013-02-21 00:52:56

+0

那麼現在我應該怎麼做才能在他的形象上畫出圓圈? – userXktape 2013-02-21 01:17:04

+1

「但沒有任何反應」真的沒有幫助,你知道嗎?現在有一個人知道你在做什麼,因此在你設法改進問題之前,沒有其他人能夠幫助你。 – mmgp 2013-02-21 01:21:16

回答

0

假設你的視頻源是一個正常的攝像頭。我不確定霍夫能否工作。我試過了,它不起作用,如果我花更多時間,它可以工作。但是這裏有一種基於顏色的方法可以幫助我。結果:https://s3-ap-southeast-1.amazonaws.com/kzl/eye.png

#include "stdafx.h" 
#include "opencv2/core/core.hpp" 
#include "opencv2/imgproc/imgproc.hpp" 
#include "opencv2/highgui/highgui.hpp" 
#include <iostream> 
#include <time.h> 
#include "opencv2/objdetect/objdetect.hpp" 
using namespace cv; 
using namespace std; 


int s0=31,s1=96,s2=189, thresh=9; 
String face_cascade_name = "lbpcascade_frontalface.xml"; 
String eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml"; 
CascadeClassifier face_cascade,eyes_cascade; 

void proc(Mat frame) 
{ 
    double time = (double)getTickCount(); 
    std::vector<Rect> faces; 
    Mat frame_gray; 
    Mat eyeMask=Mat::zeros(frame.rows, frame.cols, CV_8UC1);; 
    cvtColor(frame, frame_gray, CV_BGR2GRAY); 
    equalizeHist(frame_gray, frame_gray); 

    face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2,0, Size(50, 50)); 

    for(int i = 0; i < faces.size(); i++) 
    { 
     Mat faceROI = frame_gray(faces[i]); 
     std::vector<Rect> eyes; 

     eyes_cascade.detectMultiScale(faceROI, eyes, 1.1, 2, 0 |CV_HAAR_SCALE_IMAGE, Size(30, 30)); 
     if(eyes.size() == 2) 
     { 
      Point center(faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5); 
      ellipse(frame, center, Size(faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar(255, 0, 0), 2, 8, 0); 

      for(int j = 0; j < eyes.size(); j++) 
      { 
       Rect roi = Rect(faces[i].x+eyes[j].x,faces[i].y+eyes[j].y+(eyes[j].height*.25),eyes[j].width,eyes[j].height*.75); 
       Mat eye(frame,roi); 
       Mat eyeYuv; 
       vector<Mat> eyechs; 
       cvtColor(eye,eyeYuv,CV_BGR2YUV); 
       split(eyeYuv,eyechs); 
       Mat mask= eyechs[0]; 
       equalizeHist(mask,mask); 
       threshold(mask,mask,thresh,255,CV_THRESH_BINARY_INV); 
       mask.copyTo(eyeMask(roi)); 
      } 
      imshow("mask",eyeMask); 
     } 

    } 

    cvtColor(frame, frame, CV_BGR2YUV); 
    for(int i=0;i<eyeMask.rows;i++) 
    { 
     for(int j=0;j<eyeMask.cols;j++) 
     { 
      if(eyeMask.at<uchar>(i,j)>0) 
      { 
       frame.at<Vec3b>(i,j)[0]=(frame.at<Vec3b>(i,j)[0]+s0)%255; 
       frame.at<Vec3b>(i,j)[1]=(frame.at<Vec3b>(i,j)[1]+s1)%255; 
       frame.at<Vec3b>(i,j)[2]=(frame.at<Vec3b>(i,j)[2]+s2)%255; 
      } 
     } 
    } 
    cvtColor(frame, frame, CV_YUV2BGR); 

    time = ((double)getTickCount() - time)/getTickFrequency(); 
    std::ostringstream strs; 
    strs << time; 
    std::string text = strs.str(); 

    putText(frame, text, Point(30,30), FONT_HERSHEY_SCRIPT_SIMPLEX, .7, Scalar::all(255), 2,8); 
    imshow("cam", frame); 
} 

void run() 
{ 
    namedWindow("cam", CV_WINDOW_KEEPRATIO); 
    namedWindow("ctrl", CV_WINDOW_KEEPRATIO); 
    namedWindow("mask", CV_WINDOW_KEEPRATIO); 

    createTrackbar("s0", "ctrl", &s0, 255, NULL); 
    createTrackbar("s1", "ctrl", &s1, 255, NULL); 
    createTrackbar("s2", "ctrl", &s2, 255, NULL); 
    createTrackbar("irisThresh", "ctrl", &thresh, 255, NULL); 

    VideoCapture cap; 
    cap.open(0); 
    if(!cap.isOpened()) 
    { 
     cout << "cap error\n"; 
    } 

    if(!face_cascade.load(face_cascade_name)){ cout << "face cascade error\n"; return ; }; 
    if(!eyes_cascade.load(eyes_cascade_name)){ cout << "eye cascade error\n"; return ; }; 
    Mat frame; 
    cap >> frame; 


    for(;;) 
    { 
     cap >> frame; 
     resize(frame,frame,Size(320,240)); 
     proc(frame); 
     char c = (char)waitKey(10); 
     if(c == 27) 
      break; 
    } 
} 

int main(int argc, char ** argv) 
{ 
    run(); 
    cin.ignore(1); 
    return 0; 
}