這道題目來源于網友問答,采用部落格方式回答比較容易說明。
問題:
對于這樣的圖檔
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIn5GcuQGMyQTMjJWZ0IDMlNjNjZDM3gDM1cDZzYjZ1IzN1UjZfdWbp9CXt92Yu4GZjlGbh5SZslmZxl3Lc9CX6MHc0RHaiojIsJye.png)
如何尋找上面的矩形
思路:
這個矩形的面積在所有的圖像中是最大的(除去整個圖形輪廓以外),可以嘗試從這個方面入手,再加上一些魯壯的方法
#include "stdafx.h"
#include <opencv2/opencv.hpp>
#include "GOCVHelper.h"
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, const char** argv )
{
Mat src = imread("card.png",IMREAD_COLOR);
Mat gray;
int imax = 0; //代表最大輪廓的序号
int imaxcontour = -1; //代表最大輪廓的大小
std::vector<std::vector<Point>>contours;
cvtColor(src,gray,COLOR_BGR2GRAY);
threshold(gray,gray,100,255,THRESH_OTSU);
bitwise_not(gray,gray);// 白色代表有資料
//尋找輪廓
findContours(gray,contours,CV_RETR_LIST,CV_CHAIN_APPROX_SIMPLE);
//冒泡排序,由大到小排序
VP vptmp;
for(int i=1;i<contours.size();i++){
for(int j=contours.size()-1;j>=i;j--){
if( contourArea(contours[j])> contourArea(contours[j-1])){
//swap
vptmp = contours[j-1];
contours[j-1] = contours[j];
contours[j] = vptmp;
}
}
}
//找到最後結果的時候,添加一些判斷
for (int i = 0;i<contours.size();i++)
{
if (contourArea(contours[i]) < (src.rows * src.cols)/8 )
{
drawContours(src,contours,i,Scalar(0,0,255),-1);
break;
imshow("結果",src);
waitKey();
return 0;
}
結果:
小結:
當然這裡隻是對最簡單的模闆圖檔進行了處理。如果在實際的錄影機拍攝的過程中,肯定會有其他的幹擾,需要差別對待。
目前方向:圖像拼接融合、圖像識别
聯系方式:[email protected]