我目前正在实施一种识别图像中连接组件的算法。在执行自适应阈值处理后,我创建了另一个名为sub的Mat变量,我在其中更新其值,使得包含它们的值允许我使用更新' newObject'来识别哪些像素连接到哪个像素。变量。
当我完成这个时,我的预期输出是一个相对连接的图片,具有各种强度,可以看到哪些部分连接到哪个部分。然而,我的问题是我的输出是子变量只显示黑色图像。
我通过输入print语句检查当前值来检查值是否实际发生了变化。我还在if语句中将各种打印语句放在一点,并了解到它们确实进入了我更改子变量中像素值的条件。
我的问题是当我在子变量上执行imshow函数时,为什么输出图像是黑色图像。对不起,如果我在某处犯了错误,我相对较新。
for(int i = 0; i < detected_edges.rows; i++){
for(int j = 0; j < detected_edges.cols; j++){
if(detected_edges.at<uchar>(i,j) > 0){
Point current = (i,j);
if(i-1 < 0 && j-1 < 0){
newObject = newObject + 15; //no values
sub.at<uchar>(current) = newObject;
printf("This is the value at this point: %u", sub.at<uchar>(current));
}else if(i-1 >= 0 && j-1 < 0){
Point up = (i-1,j);
if(sub.at<uchar>(up) != 0){
sub.at<uchar>(current) = sub.at<uchar>(up); //only up has value
}else{
newObject = newObject + 15; //no values
sub.at<uchar>(current) = newObject;
}
}else if(i-1 < 0 && j-1 >= 0){
Point left = (i,j-1);
if(sub.at<uchar>(left) != 0){
sub.at<uchar>(current) = sub.at<uchar>(left); //only left has value
}else{
newObject = newObject + 15; //no values
sub.at<uchar>(current) = newObject;
}
}else{
Point up = (i-1,j);
Point left = (i,j-1);
if(sub.at<uchar>(up) == 0 && sub.at<uchar>(left) == 0){
newObject = newObject + 15; //no values
sub.at<uchar>(current) = newObject;
}else if(sub.at<uchar>(up) == sub.at<uchar>(left)){ //same value
sub.at<uchar>(current) = sub.at<uchar>(up);
}else if(sub.at<uchar>(up) != sub.at<uchar>(left) && (sub.at<uchar>(up) != 0 && sub.at<uchar>(left) != 0)){
sub.at<uchar>(current) = sub.at<uchar>(up); //different values follow upper's value
}else if((sub.at<uchar>(up) != 0 && sub.at<uchar>(left) == 0)){
sub.at<uchar>(current) = sub.at<uchar>(up); //only up has value
}else if(sub.at<uchar>(up) == 0 && sub.at<uchar>(left) != 0 ){
sub.at<uchar>(current) = sub.at<uchar>(left); //only left has value
}
}
}
}
}
更新:我注意到只有我的第一行&#39; sub&#39; Mat变量由于某种原因正在改变值,即使循环上升到图像的最后一行。
答案 0 :(得分:0)
图像是黑色的原因是因为我通过制作更多的对象来浏览灰度图像(0-255)的值范围。我使用2D数组执行了不同的实现,并且已经实现了算法。