1
我想從輪廓的中心找到一個點,它是最接近黑色像素或非輪廓像素的點。我想創建一條從中心到那一點的直線。這裏的是我當前的代碼:在OpenCV和C++中從輪廓中心尋找最近的黑色像素
#include<iostream>
#include<opencv2\highgui\highgui.hpp>
#include<opencv2\imgproc\imgproc.hpp>
#include<opencv2\core\core.hpp>
#include<opencv2/opencv.hpp>
#include<opencv2/core/core.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/objdetect/objdetect.hpp>
#include<Windows.h>
#include <sstream>
using namespace cv;
using namespace std;
void on_trackbar(int, void*);
void createTrackbars();
void morphit(Mat &img);
void toggle(int);
const int MAX_NUM_OBJECTS = 500;
const int FRAME_WIDTH = 900;
const int FRAME_HEIGHT = 600;
const int MIN_OBJECT_AREA = 20 * 20;
const int MAX_OBJECT_AREA = FRAME_HEIGHT*FRAME_WIDTH/1.5;
Point middle;
int l_MIN = 30;
int l_MAX = 165;
int a_MIN = 139;
int a_MAX = 165;
int b_MIN = 136;
int b_MAX = 172;
int kerode = 2;
int kdilate = 5;
bool domorph = true;
bool showchangedframe = true;
int main(int argc, char** argv)
{
createTrackbars();
on_trackbar(0, 0);
int x, y;
Mat frame, labframe, rangeframe;
Mat newframe, newrf;
int key;
//VideoCapture cap(0);
while ((key = waitKey(30)) != 27)
{
toggle(key);
frame = imread(argv[1], 1);
newframe = imread(argv[1], 1);
//cap >> frame;
//cap >> newframe;
//flip(frame, frame, 180);
//flip(newframe, newframe, 180);
newframe = Scalar(0, 0, 0);
cvtColor(frame, labframe, COLOR_BGR2Lab);
inRange(labframe, Scalar(l_MIN, a_MIN, b_MIN), Scalar(l_MAX, a_MAX, b_MAX), rangeframe);
newrf = rangeframe.clone();
int largest_area = 0;
int largest_contour_index = 0;
vector<vector<Point> > contours;
findContours(newrf, contours, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);
vector<Moments> mu(contours.size()); //get moments
for (int i = 0; i < contours.size(); i++)
{
mu[i] = moments(contours[i], false);
}
vector<Point2f> mc(contours.size()); //get centers
for (int i = 0; i < contours.size(); i++)
{
mc[i] = Point2f(mu[i].m10/mu[i].m00, mu[i].m01/mu[i].m00);
}
for (int i = 0; i < contours.size(); i++) //iterate through each contour.
{
double a = contourArea(contours[i], false); //Find the area of contour
if (a>largest_area)
{
largest_area = a;
largest_contour_index = i; //Store the index of largest contour
}
}
drawContours(newframe, contours, largest_contour_index, CV_RGB(255, 0, 0), CV_FILLED);
circle(newframe, mc[largest_contour_index], 5, CV_RGB(255, 255, 0), -1, 8, 0);
imshow("Detected", newframe);
if (showchangedframe)
imshow("Camera", frame);
else
imshow("Camera", rangeframe);
}
}
void on_trackbar(int, void*)
{
if (kerode == 0)
kerode = 1;
if (kdilate == 0)
kdilate = 1;
}
void createTrackbars()
{
String trackbarWindowName = "TrackBars";
namedWindow(trackbarWindowName, WINDOW_NORMAL);
createTrackbar("l_MIN", trackbarWindowName, &l_MIN, l_MAX, on_trackbar);
createTrackbar("l_MAX", trackbarWindowName, &l_MAX, l_MAX, on_trackbar);
createTrackbar("a_MIN", trackbarWindowName, &a_MIN, a_MAX, on_trackbar);
createTrackbar("a_MAX", trackbarWindowName, &a_MAX, a_MAX, on_trackbar);
createTrackbar("b_MIN", trackbarWindowName, &b_MIN, b_MAX, on_trackbar);
createTrackbar("b_MAX", trackbarWindowName, &b_MAX, b_MAX, on_trackbar);
createTrackbar("Erosion", trackbarWindowName, &kerode, 31, on_trackbar);
createTrackbar("Dilation", trackbarWindowName, &kdilate, 31, on_trackbar);
}
void morphit(Mat &img)
{
erode(img, img, getStructuringElement(MORPH_RECT, Size(kerode, kerode)));
dilate(img, img, getStructuringElement(MORPH_RECT, Size(kdilate, kdilate)));
}
void toggle(int key)
{
if (key == 'r')
showchangedframe = !showchangedframe;
}
我的輸出變爲由以下 Output
我想是創建如下:在advance..much幫助 Desired Output
由於是必要的。
爲什麼不提取圖像的邊緣圖,然後檢查沿着邊緣圖的中心和白色像素之間的最小「歐幾里得距離」。距離最短的點會給你想要的東西。準確地說,您可能必須將點移動一個像素(上/下/左/右)。 –
@RickM。或者在找到輪廓之前擴大圖像,而不是試圖找出移動的方向。 – beaker
@RickM。感謝您的建議。對不起,但你會如何建議我提取邊緣地圖? – IzaMA