当前位置:网站首页>Object extraction_ nanyangjx
Object extraction_ nanyangjx
2022-06-26 08:40:00 【Algorithmic Pilgrim】
Ideas :

Code :
#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:/gloomyfish/case3.png", IMREAD_GRAYSCALE);
if (src.empty()) {
printf("could not load image...\n");
return -1;
}
namedWindow("input image", CV_WINDOW_AUTOSIZE);
imshow("input image", src);
// Two valued
threshold(src, binary, 0, 255, THRESH_BINARY | THRESH_OTSU);
imshow("binary image", binary);
// Morphological operation
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++) {
// Area filtering
double area = contourArea(contours[t]);
if (area < 100) continue;
// Horizontal vertical ratio filtration
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;
}
边栏推荐
- Microcontroller from entry to advanced
- Compiling owncloud client on win10
- Tokenizer description in Bert
- Koa_ mySQL_ Integration of TS
- Text to SQL model ----irnet
- Convex optimization of quadruped
- X-VLM多模态模型解读
- Use a switch to control the lighting and extinguishing of LEP lamp
- How to correctly PIP install pyscipopt
- 2020-10-29
猜你喜欢

Fabrication of modulation and demodulation circuit

Can the encrypted JS code and variable name be cracked and restored?

三菱PLC若想实现以太网无线通讯,需要具备哪些条件?

SQL learning experience (II): question brushing record

(2) Buzzer

(1) Turn on the LED

STM32 porting mpu6050/9250 DMP official library (motion_driver_6.12) modifying and porting DMP simple tutorial

Opencv learning notes 3

STM32 project design: smart home system design based on stm32

Method of measuring ripple of switching power supply
随机推荐
Interpretation of x-vlm multimodal model
Relevant knowledge of DRF
三菱PLC若想实现以太网无线通讯,需要具备哪些条件?
Cause analysis of serial communication overshoot and method of termination
(4) Independent key
Convex optimization of quadruped
STM32 porting mpu6050/9250 DMP official library (motion_driver_6.12) modifying and porting DMP simple tutorial
Jupyter的安装
Partial summary of 45 lectures on geek time MySQL
KNN resolution
多台三菱PLC如何实现无线以太网高速通讯?
Idea uses regular expressions for global substitution
The best time to buy and sell stocks to get the maximum return
Remote centralized control of distributed sensor signals using wireless technology
I Summary Preface
Learning signal integrity from scratch (SIPI) -- 3 challenges faced by Si and Si based design methods
STM32 project design: an e-reader making tutorial based on stm32f4
And are two numbers of S
Calculation of decoupling capacitance
51 single chip microcomputer project design: schematic diagram of timed pet feeding system (LCD 1602, timed alarm clock, key timing) Protues, KEIL, DXP