opencv c++ 질문 도형들의 중심값을 구해서 그 도형들 사이에 선을 그리기
조회수 365회
도형들의 중심값을 구해서 그 도형들 사이에 선을 이을라하는데 중심선을 구하는게 이게아닌가여
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
string title = "트랙바 이벤트";
Mat image, dst;
void onChange(int Threshold, void* userdata)
{
Point pt;
for (pt.y = 0; pt.y < image.rows; pt.y++)
for (pt.x = 0; pt.x < image.cols; pt.x++)
{
if (image.at<uchar>(pt.y, pt.x) < Threshold)
dst.at<uchar>(pt.y, pt.x) = 0;
else
dst.at<uchar>(pt.y, pt.x) = 255;
}
imshow(title, dst);
}
int main()
{
int pos = 128;
image = imread("hwfig2-2.jpg", IMREAD_GRAYSCALE);
Mat img2(image.rows, image.cols, CV_8UC3);
int acount = 0,
bcount = 0,
ccount = 0,
dcount = 0,
count = 0;
double axsum = 0, aysum = 0,
bxsum = 0, bysum = 0,
cxsum = 0, cysum = 0,
dxsum = 0, dysum = 0;
cv::Point_<double> z_1_1, z_1_2,
z_2_1, z_2_2,
z_3_1, z_3_2,
z_4_1, z_4_2;
Point pt;
for (pt.y = 0; pt.y < image.rows; pt.y++)
for (pt.x = 0; pt.x < image.cols; pt.x++)
{
if (image.at<uchar>(pt.y, pt.x) < 150) {
img2.at<Vec3b>(pt.y, pt.x) = Vec3b(0, 255, 255);
axsum += pt.x;
aysum += pt.y;
acount++;
}
else if (image.at<uchar>(pt.y, pt.x) > 150 && image.at<uchar>(pt.y, pt.x) < 180) {
img2.at<Vec3b>(pt.y, pt.x) = Vec3b(0, 255, 0);
bxsum += pt.x;
bysum += pt.y;
bcount++;
}
else if (image.at<uchar>(pt.y, pt.x) > 180 && image.at<uchar>(pt.y, pt.x) < 230) {
img2.at<Vec3b>(pt.y, pt.x) = Vec3b(0, 0, 255);
cxsum += pt.x;
cysum += pt.y;
ccount++;
}
else if (image.at<uchar>(pt.y, pt.x) > 230 && image.at<uchar>(pt.y, pt.x) < 250) {
img2.at<Vec3b>(pt.y, pt.x) = Vec3b(255, 0, 0);
dxsum += pt.x;
dysum += pt.y;
dcount++;
}
else {
img2.at<Vec3b>(pt.y, pt.x) = Vec3b(255, 255, 255);
}
}
z_1_1 = (Point)axsum / acount,(Point)aysum / acount;
z_2_1 = (Point)bxsum / bcount,(Point)bysum / bcount;
z_3_1 = (Point)cxsum / ccount,(Point)cysum / ccount;
z_4_1 = (Point)dxsum / dcount,(Point)dysum / dcount;
line(img2, z_1_1, z_2_1, Scalar(0, 0, 0), LINE_4, 1);
line(img2, z_2_1, z_3_1, Scalar(0, 0, 0), LINE_4, 1);
line(img2, z_3_1, z_4_1, Scalar(0, 0, 0), LINE_4, 1);
line(img2, z_4_1, z_1_1, Scalar(0, 0, 0), LINE_4, 1);
imshow("img1", image);
imshow("img2", img2);
waitKey(0);
return 0;
}
댓글 입력