当前位置:网站首页>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;
}
边栏推荐
- Formula understanding in quadruped control
- Idea update
- (5) Matrix key
- What is Qi certification Qi certification process
- Opencv learning notes 3
- Exploration of webots and ROS joint simulation (I): software installation
- Recognize the interruption of 80s51
- Cause analysis of serial communication overshoot and method of termination
- Relevant knowledge of DRF
- Is it safe to open an account in flush,
猜你喜欢

MPC learning notes (I): push MPC formula manually

HEVC学习之码流分析

Text to SQL model ----irnet

Recovering the system with Clonezilla USB disk

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

Relationship extraction --r-bert

Simulation of parallel structure using webots

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

Whale conference one-stop intelligent conference system helps organizers realize digital conference management

(4) Independent key
随机推荐
Vs2019-mfc setting edit control and static text font size
Two ways to realize time format printing
Using transformers of hugging face to realize text classification
nn. Modulelist and nn Sequential
Discrete device ~ resistance capacitance
Can the encrypted JS code and variable name be cracked and restored?
Method of measuring ripple of switching power supply
Simulation of parallel structure using webots
(1) Turn on the LED
First character that appears only once
Partial summary of 45 lectures on geek time MySQL
在 KubeSphere 部署 Wiki 系统 wiki.js 并启用中文全文检索
51 single chip microcomputer project design: schematic diagram of timed pet feeding system (LCD 1602, timed alarm clock, key timing) Protues, KEIL, DXP
2020-10-17
Record the problem yaml file contains Chinese message 'GBK' error
Summary of common instructions for arm assembly
drf的相关知识
[已解决]setOnNavigationItemSelectedListener()被弃用
Tokenizer description in Bert
Learn signal integrity from zero (SIPI) - (1)