常用的一些记录

来源:互联网 发布:大蚂蚁软件 编辑:程序博客网 时间:2024/05/16 17:16

开运算:先腐蚀,再膨胀

可以清除一些小东西(亮的),放大局部低亮度的区域


闭运算:先膨胀,再腐蚀

可以清除小黑点




轮廓区域矩形绘制

Rect rect = boundingRect((Mat)contours[i]);

CvScalar color = CV_RGB(0, 255, 0);

rectangle(testMat, rect, color, 2);


绘制轮廓区域 i=-1直接画全部     3==CV_FILLED时就是反向填充好轮廓区域

color = CV_RGB(0, 0, 255);
cv::drawContours(testMat, contours, i, cv::Scalar(0, 0, 255), 3);




//作用是取一个轮廓独立取出来, 然后用RECT包出来,

//这样做的目的防止RECT放大时会把别的东西给包含进来,这个不管怎么样RECT里面只有轮廓图
void m_mask(Mat src, Mat &dst, vector<vector<Point>> contours, int i)
{
namedWindow("旋转之前-1", WINDOW_NORMAL);
imshow("旋转之前-1", src);
Mat tmpMat = src.clone();

Mat mask(tmpMat.size(), CV_8U, Scalar(0)); //遮罩图层 
cv::drawContours(mask, contours, i, Scalar(255), CV_FILLED); //在遮罩图层上,用白色像素填充轮廓  255=ffff

Mat crop(tmpMat.rows, tmpMat.cols, CV_8UC3);

tmpMat.copyTo(crop, mask);//将原图像拷贝进遮罩图层  

Rect rect = boundingRect((Mat)contours[i]);
dst = crop(rect);

//namedWindow("独立取出轮廓部份", WINDOW_NORMAL);
//imshow("独立取出轮廓部份", dst);

m_rotate(dst, dst);
}


//函数作用,将图旋转摆正并且传回
void m_rotate(Mat src, Mat &dst)
{
// Mat srcImg = imread("F://222.jpg");
namedWindow("旋转之前", WINDOW_NORMAL);
imshow("旋转之前", src);

Mat srcImg = src;

Mat gray, binImg;
//灰度化
cvtColor(srcImg, gray, COLOR_RGB2GRAY);
//imshow("灰度图", gray);
//二值化
threshold(gray, binImg, 100, 255, CV_THRESH_BINARY);
//imshow("二值化", binImg);

vector<vector<Point> > contours;
vector<Rect> boundRect(contours.size());
//注意第5个参数为CV_RETR_EXTERNAL,只检索外框  
findContours(binImg, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE); //找轮廓
cout << contours.size() << endl;
for (int i = 0; i < contours.size(); i++)
{
//需要获取的坐标  
CvPoint2D32f rectpoint[4];
CvBox2D rect = minAreaRect(Mat(contours[i]));

cvBoxPoints(rect, rectpoint); //获取4个顶点坐标  
//与水平线的角度  
float angle = rect.angle;
cout << angle << endl;

int line1 = sqrt((rectpoint[1].y - rectpoint[0].y)*(rectpoint[1].y - rectpoint[0].y) + (rectpoint[1].x - rectpoint[0].x)*(rectpoint[1].x - rectpoint[0].x));
int line2 = sqrt((rectpoint[3].y - rectpoint[0].y)*(rectpoint[3].y - rectpoint[0].y) + (rectpoint[3].x - rectpoint[0].x)*(rectpoint[3].x - rectpoint[0].x));
//rectangle(binImg, rectpoint[0], rectpoint[3], Scalar(255), 2);
//面积太小的直接pass
if (line1 * line2 < 600)
{
continue;
}


//为了让正方形横着放,所以旋转角度是不一样的。竖放的,给他加90度,翻过来  
if (line1 > line2)
{
angle = 90 + angle;
}

//新建一个感兴趣的区域图,大小跟原图一样大  
Mat RoiSrcImg(srcImg.rows, srcImg.cols, CV_8UC3); //注意这里必须选CV_8UC3
RoiSrcImg.setTo(0); //颜色都设置为黑色  
//imshow("新建的ROI", RoiSrcImg);
//对得到的轮廓填充一下  
drawContours(binImg, contours, -1, Scalar(255), CV_FILLED);

//抠图到RoiSrcImg
srcImg.copyTo(RoiSrcImg, binImg);

namedWindow("旋转轮廓图", WINDOW_NORMAL);
imshow("旋转轮廓图", RoiSrcImg);

//再显示一下看看,除了感兴趣的区域,其他部分都是黑色的了  
//namedWindow("RoiSrcImg", 1);
//imshow("RoiSrcImg", RoiSrcImg);

//创建一个旋转后的图像  
Mat RatationedImg(RoiSrcImg.rows, RoiSrcImg.cols, CV_8UC1);
RatationedImg.setTo(0);
//对RoiSrcImg进行旋转  
Point2f center = rect.center;  //中心点  
Mat M2 = getRotationMatrix2D(center, angle, 1);//计算旋转加缩放的变换矩阵 
warpAffine(RoiSrcImg, RatationedImg, M2, RoiSrcImg.size(), 1, 0, Scalar(0));//仿射变换 

namedWindow("旋转之后", WINDOW_NORMAL);
imshow("旋转之后", RatationedImg);

//imwrite("F://333.jpg", RatationedImg); //将矫正后的图片保存下来
dst = RatationedImg;
waitKey(0);
}
}