我正在将图像从RGB转换为HSV,但我无法在特定像素处提取色调部分。 我尝试了三个代码,而不是它们。
第一种方式(错误进入“hue.at(j,i)”):
int detectHue(Mat & result, int x_start, int x_stop, int y_start, int y_stop, Scalar & color){
Mat hsv;
cvtColor(result, hsv, CV_RGB2HSV);
vector<Mat> channels;
split(hsv, channels);
Mat hue = channels[0];
int h_trail_x = result.cols, h_trail_y = result.rows;
bool loop = true;
for(int j = y_start; j < y_stop && loop; j+=2){
for(int i = x_start; i < x_stop && loop; i+=2){
if(hue.at<uchar>(j,i) >= 60 && hue.at<uchar>(j,i) <= 120){
h_trail_x = i;
h_trail_y = j;
loop = false;
}
}
}
// do something
}
第二种方式(错误来自“hsv.at(j,i)”):
int detectHue(Mat & result, int x_start, int x_stop, int y_start, int y_stop, Scalar & color){
Mat hsv;
cvtColor(result, hsv, CV_RGB2HSV);
int h_trail_x = result.cols, h_trail_y = result.rows;
bool loop = true;
for(int j = y_start; j < y_stop && loop; j+=2){
for(int i = x_start; i < x_stop && loop; i+=2){
int hue = hsv.at<Vec3b>(j,i).val[0];
if(hue >= 60 && hue <= 120){
h_trail_x = i;
h_trail_y = j;
loop = false;
}
}
}
//do something
}
第三种方式(随机错误发生在“hsv.col(j).row(i).data [2]”。代码有时会运行,而其他时候会失败):
int detectHue(Mat & result, int x_start, int x_stop, int y_start, int y_stop, Scalar & color){
Mat hsv;
cvtColor(result, hsv, CV_RGB2HSV);
int h_trail_x = result.cols, h_trail_y = result.rows;
bool loop = true;
for(int j = y_start; j < y_stop && loop; j+=2){
for(int i = x_start; i < x_stop && loop; i+=2){
//int hue = hsv.at<Vec3b>(j,i).val[0];
int hue = hsv.col(j).row(i).data[0];
int sat = hsv.col(j).row(i).data[1];
int lum = hsv.col(j).row(i).data[2];
if(hue >= 60 && hue <= 120 && sat > 20 && lum < 100){
cout<<"found\n";
h_trail_x = i;
h_trail_y = j;
loop = false;
}
}
}
// do something
}
错误讯息为:OpenCV Error: Assertion failed (0 <= _rowRange.start && _rowRange.start <= _rowRange.end && _rowRange.end <= m.rows) in cv::Mat::Mat, file ..\..\..\..\opencv\modules\core\src\matrix.cpp, line 284
我正在检查x_start&gt; 0等...检查界限。
输入图像源:WebCam。
图像类型(Mat result
):CV_8UC3
请纠正我.. 提前谢谢!