2012-01-06 78 views
2

我需要将3通道图像转换为1通道才能使用霍夫圆变换。在拍摄图像从相机我的笔记本电脑的GBR:如何将3通道IplImage转换为8位?

// Laptop integrated camera (Dell inspiron 5010): 
CvCapture* camera = cvCreateCameraCapture(0); 
// ... 
IplImage* image=cvQueryFrame(camera); 
// Error: 
CvSeq* results = cvHoughCircles(image, storage, CV_HOUGH_GRADIENT, 2, image->width/10); 
// ... 

回答

4

您可能希望只使用cvCvtColor

IplImage* dst = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 1); 
cvCvtColor(src, dst, CV_RGB2GRAY); 

这会将3通道图像转换为单通道灰度图像。另外,如果你早在你的项目中,我会推荐使用C++ API,因为它比C API更“笨拙”。

你就可以用下面的代码同类型节目:

#include <opencv2/core/core.hpp> 
#include <opencv2/highgui/highgui.hpp> 
#include <opencv2/imgproc/imgproc.hpp> 

using namespace std; 
using namespace cv; 

int main(int argc, char** argv) 
{ 
    Mat frame, gray; 
    VideoCapture video(0); 

    if(video.isOpened()) 
    { 
     int key = 0; 
     do 
     { 
      video >> frame; 
      if(frame.empty()) 
      { 
       break; 
      } 

      cvtColor(frame, gray, CV_BGR2GRAY); 

      // smooth it, otherwise a lot of false circles may be detected 
      GaussianBlur(gray, gray, Size(9, 9), 2, 2); 
      vector<Vec3f> circles; 
      HoughCircles(gray, circles, CV_HOUGH_GRADIENT, 
         2, gray.rows/4, 200, 100); 
      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]); 
       // draw the circle center 
       circle(frame, center, 3, Scalar(0,255,0), -1, 8, 0); 
       // draw the circle outline 
       circle(frame, center, radius, Scalar(0,0,255), 3, 8, 0); 
      } 

      namedWindow("circles", 1); 
      imshow("circles", frame); 
      key = waitKey(33); // 30Hz video assumed... 
     } while((char)key != 27); // press ESC to exit 
    } 

    return 0; 
} 

干净多了没有所有的指针同治的:)

0

使用cvSplit:来自

void splitMyImage(IplImage* src){ 
    // Allocate image planes 
    IplImage* r = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 1); 
    IplImage* g = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 1); 
    IplImage* b = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 1); 
    // Split image onto the color planes 
    cvSplit(src, r, g, b, NULL); 
} 

http://dasl.mem.drexel.edu/~noahKuntz/openCVTut4.html