2011-10-22 2 views
1

나는 발견 된 서클과 함께 이미지를 가지고 있으며이 서클의 값을 HSV 형식으로 읽으려고합니다. 나는 HSV가 아닌 BGR 값을 얻을 수 있습니다. bgr 값을 얻은 후에 hsv로 bgr을 변환 할 수있는 방법이 있습니까?opencv에서 감지 된 서클에서 HSV 값을 읽음

#include <stdio.h> 
#include <cv.h> 
#include <highgui.h> 
#include <math.h> 

int main(int argc, char** argv) 
{ 
    //load image from directory 
    IplImage* img = cvLoadImage("C:\\Users\\Nathan\\Desktop\\SnookerPic.png"); 


    IplImage* gray = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1); 
    CvMemStorage* storage = cvCreateMemStorage(0); 

    //covert to grayscale 
    cvCvtColor(img, gray, CV_BGR2GRAY); 

    // This is done so as to prevent a lot of false circles from being detected 
    cvSmooth(gray, gray, CV_GAUSSIAN, 7, 7); 

    IplImage* canny = cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1); 
    IplImage* rgbcanny = cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,3); 
    cvCanny(gray, canny, 50, 100, 3); 

    //detect circles 
    CvSeq* circles = cvHoughCircles(gray, storage, CV_HOUGH_GRADIENT, 1, 35.0, 75, 60,0,0); 
    cvCvtColor(canny, rgbcanny, CV_GRAY2BGR); 

    //draw all detected circles 
    for (int i = 0; i < circles->total; i++) 
    { 
     // round the floats to an int 
     float* p = (float*)cvGetSeqElem(circles, i); 
     cv::Point center(cvRound(p[0]), cvRound(p[1])); 
     int radius = cvRound(p[2]); 
     uchar* ptr; 
     ptr = cvPtr2D(img, center.y, center.x, NULL); 
     printf("B: %d G: %d R: %d\n", ptr[0],ptr[1],ptr[2]); 
     //CvScalar c; 
     //if(center.x > 0 && center.x < 1280 && center.y > 0 && center.y < 720) 
     //{ 
     //c = cvGet2D(img,center.x, center.y);//colour of circle 
     //} 

     // draw the circle center 
     cvCircle(img, center, 3, CV_RGB(0,255,0), -1, 8, 0); 

     // draw the circle outline 
     cvCircle(img, center, radius+1, CV_RGB(0,0,255), 2, 8, 0); 

     //display coordinates 
     printf("x: %d y: %d r: %d\n",center.x,center.y, radius); 

    } 

    //create window 
    //cvNamedWindow("circles", 1); 
    cvNamedWindow("SnookerImage", 1); 
    //show image in window 
    //cvShowImage("circles", rgbcanny); 
    cvShowImage("SnookerImage", img); 

    cvSaveImage("out.png", img); 
    //cvDestroyWindow("SnookerImage"); 
    //cvDestroyWindow("circles"); 
    //cvReleaseMemStorage("storage"); 
    cvWaitKey(0); 

    return 0; 
} 
+0

가능한 [RGB를 HSV로 변환하고 HSV를 RGB로 변환하는 알고리즘?] (http://stackoverflow.com/questions/3018313/algorithm-to-convert-rgb-to-hsv-and-hsv-to- rgb) (그리고 많은 다른 많은 사람들이) – millimoose

답변

1

OpenCV의 아래 소스 코드 cvCvtColor()라는 컬러 공간 사이의 변환 유틸리티 기능이 있습니다.

+0

나는 색 공간 변환을 이해합니다. 그 hsv 값을 추출보다 rgb –

+0

@ user170705와 메신저 : 메신저에 대한 링크가있는 변환에 대한 C 코드에 대한 답변과 대답이 있습니다. – millimoose