因此,我正在研究一個程序,該程序可以在飛機上飛行時檢測地面上不同形狀和顏色的標誌。它們都是未知的,但顏色會比周圍的地形更加強烈,並且形狀將是基本的幾何形狀。這包括正方形,三角形和圓,以及平行四邊形和十字。不同顏色的某些物體的實時形狀檢測
我已經得到我的程序來檢測形狀,但它非常不穩定,並且不僅僅關注我想要的對象。它可以發現陰影和光線以及其他任何你能想象到的形狀。這可能是因爲我在整個框架上運行邊緣檢測而不是感興趣的對象。
這裏有我的想法來解決這個問題:
1)檢測的顏色,然後找到周圍的顏色(我不知道這是可能的)形狀。
2)匹配形狀與此人匹配數字的相同方式 - >http://blog.damiles.com/2008/11/basic-ocr-in-opencv/。如果實施到我的程序中,我發現與此方法相同的問題。我猜測它會嘗試將所有內容與形狀相匹配。
3)找到一種方法來裁剪高強度框架的一部分,然後對其進行形狀識別。
我的問題是,我的下一步應該是什麼?有沒有辦法讓我的程序專注於感興趣的對象而不是它看到的所有內容? (請記住,我希望最終記錄對象的形狀和顏色以及其內部的ascii字符。)
以下是我到目前爲止的內容。
#include <iostream>
#include <cv.h>
#include <highgui.h>
using namespace std;
int main()
{
//show the original image
cvNamedWindow("Raw",CV_WINDOW_NORMAL);
CvCapture* capture = cvCaptureFromCAM(0);
IplImage* img;
while(1)
{
img = cvQueryFrame(capture);
//converting the original image into grayscale
IplImage* imgGrayScale = cvCreateImage(cvGetSize(img), 8, 1);
cvCvtColor(img,imgGrayScale,CV_BGR2GRAY);
//thresholding the grayscale image to get better results
cvThreshold(imgGrayScale,imgGrayScale,125,255,CV_THRESH_BINARY);
CvSeq* contours; //hold the pointer to a contour in the memory block
CvSeq* result; //hold sequence of points of a contour
CvMemStorage *storage = cvCreateMemStorage(0); //storage area for all contours
//finding all contours in the image
cvFindContours(imgGrayScale, storage, &contours, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));
//iterating through each contour
while(contours)
{
//obtain a sequence of points of contour, pointed by the variable 'contour'
result = cvApproxPoly(contours, sizeof(CvContour), storage, CV_POLY_APPROX_DP, cvContourPerimeter(contours)*0.02, 0);
//if there are 3 vertices in the contour(It should be a triangle)
if(result->total==3)
{
//iterating through each point
CvPoint *pt[3];
for(int i=0;i<3;i++){
pt[i] = (CvPoint*)cvGetSeqElem(result, i);
}
//////////drawing lines around the triangle
cvLine(img, *pt[0], *pt[1], cvScalar(255,0,0),4);
cvLine(img, *pt[1], *pt[2], cvScalar(255,0,0),4);
cvLine(img, *pt[2], *pt[0], cvScalar(255,0,0),4);
cout << "\nTriangle\n";
}
//if there are 4 vertices in the contour(It should be a quadrilateral)
else if(result->total==4)
{
//iterating through each point
CvPoint *pt[4];
for(int i=0;i<4;i++){
pt[i] = (CvPoint*)cvGetSeqElem(result, i);
}
//drawing lines around the quadrilateral
//cvLine(img, *pt[0], *pt[1], cvScalar(0,255,0),4);
//cvLine(img, *pt[1], *pt[2], cvScalar(0,255,0),4);
//cvLine(img, *pt[2], *pt[3], cvScalar(0,255,0),4);
//cvLine(img, *pt[3], *pt[0], cvScalar(0,255,0),4);
cout << "\nquadrilateral\n" ;
}
//if there are 7 vertices in the contour(It should be a heptagon)
else if(result->total ==7 )
{
//iterating through each point
CvPoint *pt[7];
for(int i=0;i<7;i++){
pt[i] = (CvPoint*)cvGetSeqElem(result, i);
}
//drawing lines around the heptagon
cvLine(img, *pt[0], *pt[1], cvScalar(0,0,255),4);
cvLine(img, *pt[1], *pt[2], cvScalar(0,0,255),4);
cvLine(img, *pt[2], *pt[3], cvScalar(0,0,255),4);
cvLine(img, *pt[3], *pt[4], cvScalar(0,0,255),4);
cvLine(img, *pt[4], *pt[5], cvScalar(0,0,255),4);
cvLine(img, *pt[5], *pt[6], cvScalar(0,0,255),4);
cvLine(img, *pt[6], *pt[0], cvScalar(0,0,255),4);
}
//obtain the next contour
contours = contours->h_next;
}
//show the image in which identified shapes are marked
cvNamedWindow("Tracked");
cvShowImage("Tracked",img);
char c = cvWaitKey(33); //wait for a key press
if(c==27)
{
//cleaning up
cvDestroyAllWindows();
cvReleaseMemStorage(&storage);
cvReleaseImage(&img);
cvReleaseImage(&imgGrayScale);
break;
}
}
return 0;
}
在此先感謝您的幫助!