#include "stdio.h"
#include "cv.h"
#include "highgui.h"
#include "Math.h"
#define max_corners 4
#define C CV_PI /3
int Otsu(IplImage* src);
int main(int argc, char*argv[])
{
IplImage* img = cvLoadImage("C:\\Users\\aoe\\Desktop\\xiangmu10\\fffffffff.jpg", 0);
cvNamedWindow("img", 1);
cvShowImage("img", img);
IplImage* dst = cvCreateImage(cvGetSize(img), 8, 1);
int threshold = Otsu(img);//最大类间方差阈值分割
printf("threshold = %d\n", threshold);
cvThreshold(img, dst, threshold, 255, CV_THRESH_BINARY);
cvNamedWindow("dst", 1);
cvShowImage("dst", dst);
CvRect roi = cvRect(30, 30,120,120);//去除复杂背景
IplImage* img1 = cvCreateImage(cvGetSize(dst), dst->depth, dst->nChannels);
for (int y = 0; y < img1->height; y++)
{
for (int x = 0; x < img1->width; x++)
{
CvScalar cs = (255);
cvSet2D(img1, y, x, cs);
}
}
CvRect roi1 = cvRect(30, 30, 120, 120);
cvNamedWindow("img1");
cvShowImage("img1", img1);
cvSetImageROI(dst, roi);
cvSetImageROI(img1, roi1);
cvCopy(dst, img1);
cvResetImageROI(dst);
cvResetImageROI(img1);
cvNamedWindow("result", 1);
cvShowImage("result", img1);
IplImage*edge = cvCreateImage(cvGetSize(img1), 8, 1);//canny边缘检测
int edgeThresh = 1;
cvCanny(img1, edge, edgeThresh, edgeThresh * 3, 3);
cvNamedWindow("canny", 1);
cvShowImage("canny", edge);
int count = 0;
for (int yy = 0; yy < edge->height; yy++)//统计边缘图像中共有多少个黑色像素点
{
for (int xx = 0; xx < edge->width; xx++)
{
//CvScalar ss = (255);
double ds = cvGet2D(edge, yy, xx).val[0];
if (ds == 0)
count++;
}
}
int dianshu_threshold = (176*144-count)/
opencv——检测四边形的四个角点
最新推荐文章于 2024-04-22 15:04:50 发布