我有一个图像(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;
}发布于 2014-07-01 00:13:55
通过将线条扭曲为1 x(线长)或(线长)x 1、垂直或水平垫来提取线条。然后,您可以轻松地向下或跨像素值读取。
发布于 2014-07-01 02:58:12
具体的详细信息取决于您的程序,但是一旦单击两个点,就会填充值。在那之后做什么由你自己决定。
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);
}
}https://stackoverflow.com/questions/24485826
复制相似问题