我有一张图像(24位bmp),如下所示:
用户使用鼠标绘制一条线(此处以红色显示)。这条线可以是任何角度的任何地方。然后他点击鼠标右键或左键,除了在控制台上显示外,线上的图像像素值也存储在文件中。
我使用setMouseCallback()
来显示鼠标的位置(如下所示)。但是我需要更多帮助来理解在线上查找和存储像素值的优雅方式。请帮忙!
void CallBackFunc(int event, int x, int y, int flags, void* userdata)
{
if ( event == EVENT_LBUTTONDOWN )
{
cout << "Left button of the mouse is clicked - position (" << x << ", " << y << ")" << endl;
}
else if ( event == EVENT_RBUTTONDOWN )
{
cout << "Right button of the mouse is clicked - position (" << x << ", " << y << ")" << endl;
}
else if ( event == EVENT_MOUSEMOVE )
{
cout << "Mouse move over the window - position (" << x << ", " << y << ")" << endl;
}
}
int main(int argc, char** argv)
{
Mat img = imread("C:\\Users\\Acme\\Desktop\\image-processing\\2.bmp");
namedWindow(" Window", 1);
setMouseCallback(" Window", CallBackFunc, NULL);
imshow(" Window", img);
waitKey(0);
return 0;
}
答案 0 :(得分:1)
通过翘曲将线提取到1 x(线长)或(线长)x 1,垂直或水平Mat。然后,您可以轻松读取或跨越像素值。
答案 1 :(得分:1)
具体细节取决于您的程序,但一旦点击两个点,就会填充值。之后你做的事由你决定。
cv::Point g_points[2];
int g_pointIndex;
std::vector<cv::Vec3b> values;
bool g_allGood = false;
void onMouse(int e, int x, int y, int d, void* ud)
{
if (e != CV_EVENT_LBUTTONDOWN || g_pointIndex >= 2)
return;
g_points[g_pointIndex++] = cv::Point(x, y);
}
void main()
{
// load image as greyscale
Mat img = imread("C:\\temp\\2.png", CV_8UC1);
namedWindow("img", 1);
setMouseCallback("img", onMouse);
while (1)
{
// all points collected
if (g_pointIndex == 2 && !g_allGood)
{
/*
to save processing, i suggest you remove the mouse callback once all points
are collected. do this with: setMouseCallback("img", NULL,NULL);
*/
// create line iterator, and add pixel values to values vector
cv::LineIterator it(img, g_points[0], g_points[1]);
for (int i = 0; i < it.count; i++, ++it)
values.push_back((Vec3b)*it);
// you now have all pixel values in values;
g_allGood = true;
}
imshow("img", img);
waitKey(100);
}
}