findrect
#include <iostream> #include "opencv2/core/core.hpp" #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" using namespace std; using namespace cv; void hist_xy(Mat imgsrc,int *ax,int *ay); int main() { //getImage Mat imgsrc=imread("E:\\01project\\02find_rect\\01imageSrc\\image_39.jpg"); imwrite("E:\\01imgsrc.jpg",imgsrc); //分离rgb vector<Mat> vRGB; split(imgsrc,vRGB); Mat imgRed=Mat::zeros(imgsrc.size(),CV_8UC1); imgRed=vRGB.at(2)-vRGB.at(1); imgRed=imgRed-vRGB.at(0); for(int i=0;i<imgRed.rows;i++) { for (int j=0;j<imgRed.cols;j++) { if(!((i<imgRed.rows/4 && j<imgRed.cols/4) || (i>imgRed.rows*3/4 && j>imgRed.cols*3/4))) { imgRed.at<uchar>(i,j)=0; } } } imwrite("E:\\02imgr.jpg",imgRed); Mat imgbw=Mat::zeros(imgsrc.size(),CV_8UC1); //cv::adaptiveThreshold(imgRed,imgbw,255,CV_ADAPTIVE_THRESH_MEAN_C,CV_THRESH_BINARY,51,15); //adaptiveThreshold(imgRed,imgbw, 255, CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY_INV,151,5); cv::threshold(imgRed,imgbw,CV_THRESH_OTSU,255,THRESH_BINARY); imwrite("E:\\03imgbw.jpg",imgbw); //正交投影 int *xh=new int[imgbw.rows]; int *yh=new int[imgbw.cols]; hist_xy(imgbw,xh,yh); // for (int i=0;i<imgbw.rows;i++) // { // for (int j=0;j<imgbw.cols;j++) // { // // if (xh[i]>0) // { // for (int k=0;k<xh[i];k++) // { // imgsrc.at<Vec3b>(i,k)[0]=0; // imgsrc.at<Vec3b>(i,k)[1]=0; // imgsrc.at<Vec3b>(i,k)[2]=255; // } // } // if (yh[j]>0) // { // for (int k=0;k<yh[j];k++) // { // imgsrc.at<Vec3b>(k,j)[0]=0; // imgsrc.at<Vec3b>(k,j)[1]=0; // imgsrc.at<Vec3b>(k,j)[2]=255; // } // } // } // } // imwrite("E:\\04投影.jpg",imgsrc); //连续5个以上的像素点大于100进入条件 // Point px1=(0,0);//从中间开始寻找第一个连续5行5列>100的点 for (int i=imgbw.rows/2;i<imgbw.rows-4;i++) { if (xh[i-2]>100 && xh[i-1]>100 && xh[i]>100 && xh[i+1]>100 && xh[i+2]>100) { px1.y=i; break; } } for (int i=imgbw.cols/2;i<imgbw.cols-4;i++) { if (yh[i-2]>100 && yh[i-1]>100 && yh[i]>100 && yh[i+1]>100 && yh[i+2]>100) { px1.x=i; break; } } Point px2=(0,0);//从中间开始寻找第一个连续5行5列>100的点 for (int i=imgbw.rows/2;i>4;i--) { if (xh[i-2]>100 && xh[i-1]>100 && xh[i]>100 && xh[i+1]>100 && xh[i+2]>100) { px2.y=i; break; } } for (int i=imgbw.cols/2;i>4;i--) { if (yh[i-2]>100 && yh[i-1]>100 && yh[i]>100 && yh[i+1]>100 && yh[i+2]>100) { px2.x=i; break; } } for(int i=0;i<imgbw.rows;i++) { for (int j=0;j<imgbw.cols;j++) { if (i>px2.y && i<px1.y && j>px2.x && j<px1.x) { imgsrc.at<Vec3b>(i,j)[0]=0; imgsrc.at<Vec3b>(i,j)[1]=1; imgsrc.at<Vec3b>(i,j)[2]=255; } } } if (px1.x==0 || px1.y==0 ||px2.x==0 || px2.y==0 || abs(px2.x-px1.x)<2300 ||abs(px2.y-px1.y)<1600) { cout<<"cannot find the Rect"<<endl; getchar(); } imwrite("E:\\04投影.jpg",imgsrc); return 0; } //投影函数xy平面 void hist_xy(Mat imgsrc,int *ax,int *ay) { //int *ax=new int[imgsrc.rows]; int xtemp; //int *ay=new int[imgsrc.cols]; int ytemp; for (int i=0;i<imgsrc.rows;i++) { ax[i]=0; xtemp=0; for (int j=0;j<imgsrc.cols;j++) { if (imgsrc.at<uchar>(i,j)==255) { xtemp++; } } ax[i]=xtemp; } for (int j=0;j<imgsrc.cols;j++) { ay[j]=0; ytemp=0; for (int i=0;i<imgsrc.rows;i++) { if (imgsrc.at<uchar>(i,j)==255) { ytemp++; } } ay[j]=ytemp; } //delete ax; //delete ay; }
posted on 2016-09-26 09:59 xiaochenxi_cnblogs 阅读(226) 评论(0) 编辑 收藏 举报