图像切边和旋转
旋转
切边
//切边和旋转
#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>
using namespace cv;
using namespace std;
Mat src, gray_src, dst;
int threshold_value = 100;
int max_level = 255;
const char* output_win = "Contours Result";
const char* roi_win = "Final Result";
void FindROI(int, void*);
void Check_Skew(int, void*);
int main(int argc, char** argv) {
src = imread("D:/gloomyfish/case1r.png");
if (src.empty()) {
printf("could not load image...\n");
return -1;
}
namedWindow("input image", CV_WINDOW_AUTOSIZE);
imshow("input image", src);
namedWindow(output_win, CV_WINDOW_AUTOSIZE);
Check_Skew(0, 0);
// namedWindow(roi_win, CV_WINDOW_AUTOSIZE);
//createTrackbar("Threshold:", output_win, &threshold_value, max_level, FindROI);
// FindROI(0, 0);
waitKey(0);
return 0;
}
void Check_Skew(int, void*) {
Mat canny_output;
cvtColor(src, gray_src, COLOR_BGR2GRAY);
Canny(gray_src, canny_output, threshold_value, threshold_value * 2, 3, false);
vector<vector<Point>> contours;
vector<Vec4i> hireachy;
findContours(canny_output, contours, hireachy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
Mat drawImg = Mat::zeros(src.size(), CV_8UC3);
float maxw = 0;
float maxh = 0;
double degree = 0;
for (size_t t = 0; t < contours.size(); t++) {
RotatedRect minRect = minAreaRect(contours[t]);
degree = abs(minRect.angle);
if (degree > 0) {
maxw = max(maxw, minRect.size.width);
maxh = max(maxh, minRect.size.height);
}
}
RNG rng(12345);
for (size_t t = 0; t < contours.size(); t++) {
RotatedRect minRect = minAreaRect(contours[t]);
if (maxw == minRect.size.width && maxh == minRect.size.height) {
degree = minRect.angle;
Point2f pts[4];
minRect.points(pts);
Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
for (int i = 0; i < 4; i++) {
line(drawImg, pts[i], pts[(i + 1) % 4], color, 2, 8, 0);
}
}
}
printf("max contours width : %f\n", maxw);
printf("max contours height : %f\n", maxh);
printf("max contours angle : %f\n", degree);
imshow(output_win, drawImg);
Point2f center(src.cols / 2, src.rows / 2);
//getRotationMatrix2D 获得旋转矩阵
Mat rotm = getRotationMatrix2D(center, degree, 1.0);
Mat dst;
//warpAffine仿射变换
warpAffine(src, dst, rotm, src.size(), INTER_LINEAR, 0, Scalar(255, 255, 255));
imshow("Correct Image", dst);
}
void FindROI(int, void*) {
cvtColor(src, gray_src, COLOR_BGR2GRAY);
Mat canny_output;
Canny(gray_src, canny_output, threshold_value, threshold_value * 2, 3, false);
vector<vector<Point>> contours;
vector<Vec4i> hireachy;
findContours(canny_output, contours, hireachy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
int minw = src.cols*0.75;
int minh = src.rows*0.75;
RNG rng(12345);
Mat drawImage = Mat::zeros(src.size(), CV_8UC3);
Rect bbox;
for (size_t t = 0; t < contours.size(); t++) {
RotatedRect minRect = minAreaRect(contours[t]);
float degree = abs(minRect.angle);
if (minRect.size.width > minw && minRect.size.height > minh && minRect.size.width < (src.cols-5)) {
printf("current angle : %f\n", degree);
Point2f pts[4];
minRect.points(pts);
bbox = minRect.boundingRect();
Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
for (int i = 0; i < 4; i++) {
line(drawImage, pts[i], pts[(i + 1)%4], color, 2, 8, 0);
}
}
}
imshow(output_win, drawImage);
if (bbox.width > 0 && bbox.height > 0) {
Mat roiImg = src(bbox);
imshow(roi_win, roiImg);
}
return;
}
直线检测
//直线检测
#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>
using namespace cv;
using namespace std;
int max_count = 255;
int threshold_value = 100;
const char* output_lines = "Hough Lines";
Mat src, roiImage, dst;
void detectLines(int, void*);
void morhpologyLines(int, void*);
int main(int argc, char** argv) {
src = imread("D:/gloomyfish/case2.png", IMREAD_GRAYSCALE);
if (src.empty()) {
printf("could not load image...\n");
return -1;
}
namedWindow("input image", CV_WINDOW_AUTOSIZE);
imshow("input image", src);
namedWindow(output_lines, CV_WINDOW_AUTOSIZE);
Rect roi = Rect(10, 10, src.cols - 20, src.rows - 20);
roiImage = src(roi);
imshow("ROI image", roiImage);
// createTrackbar("threshold:", output_lines, &threshold_value, max_count, detectLines);
// detectLines(0, 0);
morhpologyLines(0, 0);
waitKey(0);
return 0;
}
void detectLines(int, void*) {
Canny(roiImage, dst, threshold_value, threshold_value * 2, 3, false);
//threshold(roiImage, dst, 0, 255, THRESH_BINARY | THRESH_OTSU);
vector<Vec4i> lines;
//霍夫直线检测
HoughLinesP(dst, lines, 1, CV_PI / 180.0, 30, 30.0, 0);
cvtColor(dst, dst, COLOR_GRAY2BGR);
for (size_t t = 0; t < lines.size(); t++) {
Vec4i ln = lines[t];
line(dst, Point(ln[0], ln[1]), Point(ln[2], ln[3]), Scalar(0, 0, 255), 2, 8, 0);
}
imshow(output_lines, dst);
}
void morhpologyLines(int, void*) {
// binary image
Mat binaryImage, morhpImage;
threshold(roiImage, binaryImage, 0, 255, THRESH_BINARY_INV | THRESH_OTSU);
imshow("binary", binaryImage);
// morphology operation 开运算
Mat kernel = getStructuringElement(MORPH_RECT, Size(20, 1), Point(-1, -1));
morphologyEx(binaryImage, morhpImage, MORPH_OPEN, kernel, Point(-1, -1));
imshow("morphology result", morhpImage);
// dilate image 膨胀
kernel = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));
dilate(morhpImage, morhpImage, kernel);
imshow("morphology lines", morhpImage);
// hough lines 霍夫直线检测
vector<Vec4i> lines;
HoughLinesP(morhpImage, lines, 1, CV_PI / 180.0, 30, 20.0, 0);
Mat resultImage = roiImage.clone();
cvtColor(resultImage, resultImage, COLOR_GRAY2BGR);
for (size_t t = 0; t < lines.size(); t++) {
Vec4i ln = lines[t];
line(resultImage, Point(ln[0], ln[1]), Point(ln[2], ln[3]), Scalar(0, 0, 255), 2, 8, 0);
}
imshow(output_lines, resultImage);
return;
}
对象提取
#include "pch.h"
#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>
using namespace cv;
using namespace std;
Mat src, binary, dst;
int main(int argc, char** argv) {
src = imread("D:/1.jpg", IMREAD_GRAYSCALE);
if (src.empty()) {
printf("could not load image...\n");
return -1;
}
namedWindow("input image", CV_WINDOW_NORMAL);
imshow("input image", src);
// 二值化
threshold(src, binary, 0, 255, THRESH_BINARY | THRESH_OTSU);
imshow("binary image", binary);
// 形态学操作 闭运算
Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));
morphologyEx(binary, dst, MORPH_CLOSE, kernel, Point(-1, -1));
imshow("close image", dst);
//开运算
kernel = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));
morphologyEx(dst, dst, MORPH_OPEN, kernel, Point(-1, -1));
imshow("open image", dst);
vector<vector<Point>> contours;
vector<Vec4i> hireachy;
findContours(dst, contours, hireachy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point());
Mat resultImage = Mat::zeros(src.size(), CV_8UC3);
Point cc;
for (size_t t = 0; t < contours.size(); t++) {
// 面积过滤
double area = contourArea(contours[t]);
if (area < 100) continue;
// 横纵比过滤
Rect rect = boundingRect(contours[t]);
float ratio = float(rect.width) / float(rect.height);
if (ratio < 1.1 && ratio > 0.9)
{
drawContours(resultImage, contours, t, Scalar(0, 0, 255), -1, 8, Mat(), 0, Point());
printf("circle area : %f\n", area);
printf("circle length : %f\n", arcLength(contours[t], true));
int x = rect.x + rect.width / 2;
int y = rect.y + rect.height / 2;
cc = Point(x, y);
circle(resultImage, cc, 2, Scalar(0, 0, 255), 2, 8, 0);
}
}
imshow("Result", resultImage);
Mat circleImage = src.clone();
cvtColor(circleImage, circleImage, COLOR_GRAY2BGR);
circle(circleImage, cc, 2, Scalar(0, 0, 255), 2, 8, 0);
imshow("Final Result", circleImage);
// detect circle
/*vector<Vec3f> myCircles;
Mat gray_result;
cvtColor(resultImage, gray_result, COLOR_BGR2GRAY);
HoughCircles(gray_result, myCircles, HOUGH_GRADIENT, 1, 7, 50, 20, 23, 100);
Mat circleImage = src.clone();
cvtColor(circleImage, circleImage, COLOR_GRAY2BGR);
for (int i = 0; i < myCircles.size(); i++) {
Vec3f circleInfo = myCircles[i];
circle(circleImage, Point(circleInfo[0], circleInfo[1]), circleInfo[2], Scalar(0, 0, 255), 1, 8, 0);
}
*/
waitKey(0);
return 0;
}
统计数量
#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>
using namespace cv;
using namespace std;
int main(int argc, char** argv) {
Mat gray_src, binary, dst;
Mat src = imread("D:/gloomyfish/case4.png");
if (src.empty()) {
printf("could not load image...\n");
return -1;
}
namedWindow("input image", CV_WINDOW_AUTOSIZE);
imshow("input image", src);
cvtColor(src, gray_src, COLOR_BGR2GRAY);
// 二值分割
threshold(gray_src, binary, 0, 255, THRESH_BINARY | THRESH_TRIANGLE);
imshow("binary image", binary);
// 形态学操作 膨胀
Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));
dilate(binary, binary, kernel, Point(-1, -1), 1);
imshow("dilate image", binary);
// 距离变换
Mat dist;
//按位取反
bitwise_not(binary, binary);
//distanceTransform,距离变换用于计算图像中每一个非零点距离自己最近的零点的距离,用于细化轮廓和提取质心
distanceTransform(binary, dist, CV_DIST_L2, 3);
//归一化
normalize(dist, dist, 0, 1.0, NORM_MINMAX);
imshow("dist image", dist);
// 阈值化二值分割
Mat dist_8u;
dist.convertTo(dist_8u, CV_8U);
//adaptiveThreshold自适应二值化
adaptiveThreshold(dist_8u, dist_8u, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, 85, 0.0);
kernel = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));
dilate(dist_8u, dist_8u, kernel, Point(-1, -1), 2);
imshow("dist-binary", dist_8u);
// 连通区域计数
vector<vector<Point>> contours;
findContours(dist_8u, contours, CV_RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
// draw result
Mat markers = Mat::zeros(src.size(), CV_8UC3);
RNG rng(12345);
for (size_t t = 0; t < contours.size(); t++) {
drawContours(markers, contours, static_cast<int>(t), Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)),
-1, 8, Mat());
}
printf("number of corns : %d", contours.size());
imshow("Final result", markers);
waitKey(0);
return 0;
}
透视变换
#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>
using namespace cv;
using namespace std;
int main(int argc, char** argv) {
Mat src = imread("D:/gloomyfish/case5.png");
if (src.empty()) {
printf("could not load image...\n");
return 0;
}
namedWindow("input image", CV_WINDOW_AUTOSIZE);
imshow("input image", src);
// 二值处理
Mat gray_src, binary, dst;
cvtColor(src, gray_src, COLOR_BGR2GRAY);
threshold(gray_src, binary, 0, 255, THRESH_BINARY_INV | THRESH_OTSU);
imshow("binary image", binary);
// 形态学操作 闭运算
Mat kernel = getStructuringElement(MORPH_RECT, Size(5, 5), Point(-1, -1));
morphologyEx(binary, dst, MORPH_CLOSE, kernel, Point(-1, -1), 3);
imshow("morphology", dst);
// 轮廓发现 按位取反
bitwise_not(dst, dst, Mat());
vector<vector<Point>> contours;
vector<Vec4i> hireachy;
findContours(dst, contours, hireachy, CV_RETR_TREE, CHAIN_APPROX_SIMPLE, Point());
// 轮廓绘t制
int width = src.cols;
int height = src.rows;
Mat drawImage = Mat::zeros(src.size(), CV_8UC3);
for (size_t t = 0; t < contours.size(); t++) {
Rect rect = boundingRect(contours[t]);
if (rect.width > width / 2 && rect.width < width-5) {
drawContours(drawImage, contours, static_cast<int>(t), Scalar(0, 0, 255), 2, 8, hireachy, 0, Point());
}
}
imshow("contours", drawImage);
vector<Vec4i> lines;
Mat contoursImg;
int accu = min(width*0.5, height*0.5);
cvtColor(drawImage, contoursImg, COLOR_BGR2GRAY);
//霍夫直线查找
HoughLinesP(contoursImg, lines, 1, CV_PI / 180.0, accu, accu, 0);
Mat linesImage = Mat::zeros(src.size(), CV_8UC3);
for (size_t t = 0; t < lines.size(); t++) {
Vec4i ln = lines[t];
line(linesImage, Point(ln[0], ln[1]), Point(ln[2], ln[3]), Scalar(0, 0, 255), 2, 8, 0);
}
printf("number of lines : %d\n", lines.size());
imshow("lines image", linesImage);
// 寻找与定位上下左右四条直线
int deltah = 0;
Vec4i topLine, bottomLine;
Vec4i leftLine, rightLine;
for (int i = 0; i < lines.size(); i++) {
Vec4i ln = lines[i];
deltah = abs(ln[3] - ln[1]);
if (ln[3] < height / 2.0 && ln[1] < height / 2.0 && deltah < accu - 1) {
if (topLine[3] > ln[3] && topLine[3]>0) {
topLine = lines[i];
} else {
topLine = lines[i];
}
}
if (ln[3] > height / 2.0 && ln[1] > height / 2.0 && deltah < accu - 1) {
bottomLine = lines[i];
}
if (ln[0] < width / 2.0 && ln[2] < width/2.0) {
leftLine = lines[i];
}
if (ln[0] > width / 2.0 && ln[2] > width / 2.0) {
rightLine = lines[i];
}
}
cout << "top line : p1(x, y) = " << topLine[0] << "," << topLine[1] << " p2(x, y) = " << topLine[2] << "," << topLine[3] << endl;
cout << "bottom line : p1(x, y) = " << bottomLine[0] << "," << bottomLine[1] << " p2(x, y) = " << bottomLine[2] << "," << bottomLine[3] << endl;
cout << "left line : p1(x, y) = " << leftLine[0] << "," << leftLine[1] << " p2(x, y) = " << leftLine[2] << "," << leftLine[3] << endl;
cout << "right line : p1(x, y) = " << rightLine[0] << "," << rightLine[1] << " p2(x, y) = " << rightLine[2] << "," << rightLine[3] << endl;
// 拟合四条直线方程
float k1, c1;
k1 = float(topLine[3] - topLine[1]) / float(topLine[2] - topLine[0]);
c1 = topLine[1] - k1*topLine[0];
float k2, c2;
k2 = float(bottomLine[3] - bottomLine[1]) / float(bottomLine[2] - bottomLine[0]);
c2 = bottomLine[1] - k2*bottomLine[0];
float k3, c3;
k3 = float(leftLine[3] - leftLine[1]) / float(leftLine[2] - leftLine[0]);
c3 = leftLine[1] - k3*leftLine[0];
float k4, c4;
k4 = float(rightLine[3] - rightLine[1]) / float(rightLine[2] - rightLine[0]);
c4 = rightLine[1] - k4*rightLine[0];
// 四条直线交点
Point p1; // 左上角
p1.x = static_cast<int>((c1 - c3) / (k3 - k1));
p1.y = static_cast<int>(k1*p1.x + c1);
Point p2; // 右上角
p2.x = static_cast<int>((c1 - c4) / (k4 - k1));
p2.y = static_cast<int>(k1*p2.x + c1);
Point p3; // 左下角
p3.x = static_cast<int>((c2 - c3) / (k3 - k2));
p3.y = static_cast<int>(k2*p3.x + c2);
Point p4; // 右下角
p4.x = static_cast<int>((c2 - c4) / (k4 - k2));
p4.y = static_cast<int>(k2*p4.x + c2);
cout << "p1(x, y)=" << p1.x << "," << p1.y << endl;
cout << "p2(x, y)=" << p2.x << "," << p2.y << endl;
cout << "p3(x, y)=" << p3.x << "," << p3.y << endl;
cout << "p4(x, y)=" << p4.x << "," << p4.y << endl;
// 显示四个点坐标
circle(linesImage, p1, 2, Scalar(255, 0, 0), 2, 8, 0);
circle(linesImage, p2, 2, Scalar(255, 0, 0), 2, 8, 0);
circle(linesImage, p3, 2, Scalar(255, 0, 0), 2, 8, 0);
circle(linesImage, p4, 2, Scalar(255, 0, 0), 2, 8, 0);
line(linesImage, Point(topLine[0], topLine[1]), Point(topLine[2], topLine[3]), Scalar(0, 255, 0), 2, 8, 0);
imshow("four corners", linesImage);
// 透视变换
vector<Point2f> src_corners(4);
src_corners[0] = p1;
src_corners[1] = p2;
src_corners[2] = p3;
src_corners[3] = p4;
vector<Point2f> dst_corners(4);
dst_corners[0] = Point(0, 0);
dst_corners[1] = Point(width, 0);
dst_corners[2] = Point(0, height);
dst_corners[3] = Point(width, height);
// 获取透视变换矩阵
Mat resultImage;
Mat warpmatrix = getPerspectiveTransform(src_corners, dst_corners);
//warpPerspective 仿射变换
warpPerspective(src, resultImage, warpmatrix, resultImage.size(), INTER_LINEAR);
namedWindow("Final Result", CV_WINDOW_AUTOSIZE);
imshow("Final Result", resultImage);
waitKey(0);
return 0;
}
对象提取和测量
#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>
using namespace cv;
using namespace std;
int main(int argc, char** argv) {
Mat src = imread("D:/gloomyfish/case6.jpg");
if (src.empty()) {
printf("could not load image...\n");
return -1;
}
namedWindow("input image", CV_WINDOW_AUTOSIZE);
imshow("input image", src);
Mat blurImage;
//高斯滤波
GaussianBlur(src, blurImage, Size(15, 15), 0, 0);
imshow("blur", blurImage);
//二值化
Mat gray_src, binary;
cvtColor(blurImage, gray_src, COLOR_BGR2GRAY);
threshold(gray_src, binary, 0, 255, THRESH_BINARY | THRESH_TRIANGLE);
imshow("binary", binary);
// 形态学操作 闭运算
Mat morphImage;
Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));
morphologyEx(binary, morphImage, MORPH_CLOSE, kernel, Point(-1, -1), 2);
imshow("morphology", morphImage);
// 获取最大轮廓
vector<vector<Point>> contours;
vector<Vec4i> hireachy;
findContours(morphImage, contours, hireachy, CV_RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point());
Mat connImage = Mat::zeros(src.size(), CV_8UC3);
for (size_t t = 0; t < contours.size(); t++) {
Rect rect = boundingRect(contours[t]);
if (rect.width < src.cols / 2) continue;
if (rect.width > (src.cols - 20)) continue;
double area = contourArea(contours[t]);
double len = arcLength(contours[t], true);
drawContours(connImage, contours, static_cast<int>(t), Scalar(0, 0, 255), 1, 8, hireachy);
printf("area of star could : %f\n", area);
printf("length of star could : %f\n", len);
}
imshow("result", connImage);
waitKey(0);
return 0;
}
缩小和放大
BOOL CSettingMatchDlg::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
if(h_Image.Id()==H_EMPTY_REGION)
{
return CDialog::OnMouseWheel(nFlags, zDelta, pt);
}
CRect WndRect;
HTuple Scale = 0.1;
HTuple Row0_1,Col0_1,Row1_1,Col1_1;
GetDlgItem(IDC_ST_VIEW)->GetWindowRect(&WndRect);
if(WndRect.PtInRect(pt))
{
if(MK_RBUTTON==nFlags)
{
Row0_1 = 0 ;
Row1_1 = h_Height -1;
Col0_1 = 0 ;
Col1_1 = h_Width-1 ;
set_part(h_WindowHandle,Row0_1,Col0_1,Row1_1,Col1_1 );
m_ImageRow0 = Row0_1;
m_ImageCol0 = Col0_1;
m_ImageRow1 = Row1_1;
m_ImageCol1 = Col1_1;
clear_window(h_WindowHandle);
Disp_Object();
return CDialog::OnMouseWheel(nFlags, zDelta, pt);
}
if(zDelta>0)
{
Row0_1 = m_ImageRow0 + (pt.y - WndRect.top)*Scale ;
Row1_1 = m_ImageRow1 - (WndRect.bottom - pt.y)*Scale;
Col0_1 = m_ImageCol0 + (pt.x - WndRect.left)*Scale;
Col1_1 = m_ImageCol1 - (WndRect.right - pt.x)*Scale;
if(Row0_1
{
set_part(h_WindowHandle,Row0_1,Col0_1,Row1_1,Col1_1 );
m_ImageRow0 = Row0_1;
m_ImageCol0 = Col0_1;
m_ImageRow1 = Row1_1;
m_ImageCol1 = Col1_1;
clear_window(h_WindowHandle);
Disp_Object();
}
return CDialog::OnMouseWheel(nFlags, zDelta, pt);
}
if(zDelta<0)
{
Row0_1 = m_ImageRow0 - (pt.y - WndRect.top)*Scale ;
Row1_1 = m_ImageRow1 + (WndRect.bottom - pt.y)*Scale;
Col0_1 = m_ImageCol0 - (pt.x - WndRect.left)*Scale;
Col1_1 = m_ImageCol1 + (WndRect.right - pt.x)*Scale;
set_part(h_WindowHandle,Row0_1,Col0_1,Row1_1,Col1_1 );
m_ImageRow0 = Row0_1;
m_ImageCol0 = Col0_1;
m_ImageRow1 = Row1_1;
m_ImageCol1 = Col1_1;
clear_window(h_WindowHandle);
Disp_Object();
return CDialog::OnMouseWheel(nFlags, zDelta, pt);
}
}
GetDlgItem(IDC_ST_VIEW)->SetFocus();
return CDialogEx::OnMouseWheel(nFlags, zDelta, pt);
}
在类向导中添加鼠标滚轮的消息响应函数WM_MouseWheel;并利用Halcon的Set_part来实现窗口的缩放。
PS:在VS2010里 pt 和 Point 所表示的光标位置坐标,不是同一个坐标系,在有的消息响应里用Cpoint pt
表示,其参考坐标原点为屏幕窗口左上角,而用Cpoint point表示的 ,其参考坐标原点为应用的窗口左上角