簡體   English   中英

在某些像素上繪制矩形openCV

[英]Drawing rects on certain pixels openCV

我正在嘗試定位框架的某些區域,該框架位於Ycbcr顏色空間中。 我必須根據其Y值選擇這些區域。

所以我寫了這段代碼:

Mat frame. ychannel;
VideoCapture cap(1);
int key =0;
int maxV , minV;
Point max, min;
while(key != 27){
     cap >> frame;
     cvtColor(frame,yframe,CV_RGB_YCrCb); // converting to YCbCr color space 
     extractChannel(yframe, yframe, 0); // extracting the Y channel 
     cv::minMaxLoc(yframe,&minV,&maxV,&min,&max);
     cv::threshold(outf,outf,(maxV-10),(maxV),CV_THRESH_TOZERO);
/**
Now I want to use :
cv::rectangle()
but I want to draw a rect around any pixel (see the picture bellow)that's higher than (maxV-10) 
and that during the streaming 
**/
     key = waitKey(1);
}

我畫這幅畫是跳躍的,這有助於理解我該做什么。

在此處輸入圖片說明

謝謝你的幫助。

應用閾值后,最終將得到一個包含許多已connected components的二進制圖像,如果要在每個組件周圍繪制一個矩形,則首先需要檢測這些組件。

OpenCV函數findContours就是這樣做的,將其傳遞給您的二進制圖像,它將為您提供點向量的向量,這些點將跟蹤圖像中每個組件的邊界。

cv::Mat binaryImage;
std::vector<std::vector<cv::Point>> contours;

cv::findContours(binaryImage, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE)

然后,您要做的就是找到這些點集中每一個的邊界矩形 ,並將它們繪制到輸出圖像上。

for (int i=0; i<contours.size(); ++i)
{
    cv::Rect r = cv::boundingRect(contours.at(i));
    cv::rectangle(outputImage, r, CV_RGB(255,0,0));
}

您必須找到每個連接的組件 ,並繪制其邊界框。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM