類似于提取圖片的輪廓,對(duì)圖片進(jìn)行前期的處理茁瘦,二值化纽竣,去噪點(diǎn)墓贿,提取圖片的輪廓,通過(guò)長(zhǎng)寬比進(jìn)行過(guò)濾蜓氨。
如圖:
使用opencv進(jìn)行圖片處理過(guò)程需要對(duì)圖片進(jìn)行精細(xì)的處理這樣才能保證調(diào)用算法api是可以得到比較好的結(jié)果聋袋。
- 二值化
- 形態(tài)學(xué)操作進(jìn)行去噪點(diǎn)
- 查找輪廓點(diǎn)
- 對(duì)輪廓進(jìn)行于提取圖片的輪廓,對(duì)圖片進(jìn)行前期的處理穴吹,二值化幽勒,去噪點(diǎn),提取圖片的輪廓刀荒,通過(guò)長(zhǎng)寬比進(jìn)行過(guò)濾代嗤。
如圖:
使用opencv進(jìn)行圖片處理過(guò)程需要對(duì)圖片進(jìn)行精細(xì)的處理這樣才能保證調(diào)用算法api是可以得到比較好的結(jié)果。
- 二值化
- 形態(tài)學(xué)操作進(jìn)行去噪點(diǎn)
- 查找輪廓點(diǎn)
- 對(duì)輪廓進(jìn)行過(guò)濾
#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>
using namespace cv;
using namespace std;
Mat src, dst, gray_src;
char input_image[] = "input image";
char output_image[] = "output image";
int main(int argc, char ** argv){
src = imread("case3.jpg");
if (src.empty()){
printf("colud not load image ..\n");
return -1;
}
namedWindow(input_image, CV_WINDOW_AUTOSIZE);
namedWindow(output_image, CV_WINDOW_AUTOSIZE);
imshow(input_image, src);
// 二值化
cvtColor(src, gray_src, COLOR_BGRA2GRAY);
threshold(gray_src, gray_src, 0, 255, THRESH_OTSU | THRESH_BINARY);
imshow("threshold image", gray_src);
// 閉操作連接黑點(diǎn)
Mat kernel = getStructuringElement(MORPH_RECT, Size(5, 5), Point(-1, -1));
morphologyEx(gray_src, dst, MORPH_CLOSE, kernel, Point(-1, -1));
imshow("close image", dst);
// 閉操作去毛點(diǎn)
kernel = getStructuringElement(MORPH_RECT, Size(5, 5), 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 reslutImage = 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;
// 通過(guò)寬高比進(jìn)行過(guò)濾
Rect rect = boundingRect(contours[t]);
float ratio = float(rect.width) / float(rect.height);
if (ratio<1.1&&ratio>0.9){
// -1 填充缠借,1畫(huà)圓
drawContours(reslutImage, 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(reslutImage, cc, 2, Scalar(0, 0, 255), 2, 8, 0);
}
}
imshow("Reslut", reslutImage);
Mat circleImage = src.clone();
//cvtColor(circleImage, circleImage, COLOR_GRAY2BGR);
circle(circleImage, cc,2, Scalar(0, 0, 255), 2, 8, 0);
imshow("Final Result", circleImage);
waitKey(0);
return 0;
}