我希望将以下Matlab代码更改为OpenCV。我怎样才能使用' Split'执行此操作?
我可以剪切并粘贴' for'循环原样?
L = lab(:,:,1);
a = lab(:,:,2);
b = lab(:,:,3);
[a1, b1] = size(a);
for row=1:a1
for col=1:b1
if ((a(row,col) <-0.196) && (a(row,col)>-0.3529) && (b(row,col)>0.1568) && (b(row,col)<0.392))
II(row,col) = 1 ;
else
II(row,col) = 0 ;
end
end
end
答案 0 :(得分:4)
您可以使用split功能
cv::Mat bgrImage = imread("C:/temp/cool_cat.jpg");
cv::Mat labImage;
cvtColor(bgrImage, labImage, CV_BGR2Lab);
//split the channels
vector<cv::Mat> lab_channels;
cv::split(labImage, lab_channels);
//verbose indexes for split LAB channels
const int L = 0;
const int A = 1;
const int B = 2;
//nicer than for loop IMHO
cv::Mat_<bool> t1a,t2a,t1b, t2b;
t1a = lab_channels[A] < 200;
t2a = lab_channels[A] > 100;
t1b = lab_channels[B] < 200;
t2b = lab_channels[B] > 100;
cv:Mat_<bool> II = t1a & t2a & t1b & t2b;
答案 1 :(得分:0)
下面的代码段可以让你关闭:
vector<Mat> channels;
split(lab, channels);
Mat II(lab.size(), CV_8UC1);
Mat a(channels[1]);
Mat b(channels[2]);
for (int r=0;r<lab.rows;r++)
for (int c=0;c<lab.cols;c++)
{
if ((a.at<float>(r,c) < -0.196f) &&
(a.at<float>(r,c)> -0.3529f) &&
(b.at<float>(r,c) > 0.1568f) &&
(b.at<float>(r,c) < 0.392f) ) {
II.at<uchar>(r,c) = 255;
} else {
II.at<uchar>(r,c) = 0 ;
}
}
请注意,此代码段附带了许多假设: