2013-08-23 39 views
1

我正在使用按位运算符&,而不是1和0的结果,我得到255和0.可能是什么原因? 代码是按位运算符返回255

cv::Mat TL_k_idx = ((orientation_map.col(0)<=(quad_coords[0]+quad_coords[2])/2) & (orientation_map.col(1)<=(quad_coords[1]+quad_coords[3])/2)); 
cout<<TL_k_idx; 

The output of TL_k_idx is: 
255 255 255 255 0 0............ 

的orientation_map是垫的数据类型,则quad_coords是一种array.What我做错了什么?

,同时使用逻辑运算符& &我得到一个错误

error: no match for ‘operator&&’ in ‘cv::operator<=(const cv::Mat&, double)((double)((* 
(quad_coords) + *(quad_coords + 8u))/2)) && cv::operator<=(const cv::Mat&, double)((double)((* 
(quad_coords + 4u) + *(quad_coords + 12u))/2))’| 

回答

4

你不应该指望按位为0或1。

这里的东西如何能是255:

a = 255 & 255 // then a = 255; 

这里有几个例子

Example 1 11111111 & 11111111 = 11111111 or 255 

Example 2: 01010101 & 00000111 = 101 or 5. 

http://en.wikipedia.org/wiki/Bitwise_operations_in_C

+0

对不起然后我觉得我不喜欢使用逻辑&&我试图使用逻辑AND &&但它给出'错误不匹配运算符&&' – user1583647

2

您上天然的(生的)C/C++类型和OpenCV类混合操作。

从您的问题中,我了解到您要创建一个与您的方向图的第一列和第二列尺寸相同的列向量,然后填充一些结果。这里有一个建议:

/* Create the output as the copy of the 1st col */ 
cv::Mat TL_k_idx = orientation_map.col(0).clone(); 

/* Now, fill it with the correct result by looping over the elements */ 
for (row = 0; row < TL_k_idx.rows; ++i) 
{ 
    float col0_elem = TL_k_idx.at<float>(row); // I assume you have float data; 
               // Change according to your type. 
    TL_k_idx.at<float>(row) = (col0_elem <=(quad_coords[0]+quad_coords[2])/2)) && 
          (orientation_map.col(1).at<float>(row)<=(quad_coords[1]+quad_coords[3])/2)); 
} 

这个版本没有被优化(我会用直接指向的数据。例如在我自己的代码),但这里的目的是展示多样,简便的方法在OpenCV的处理矩阵数据。

2

OpenCV总是给出255和0作为逻辑运算的结果,它使用C/C++的假设,即所有非0的值都是“真”。它用户友好,当你需要可视化结果。如果你需要0和1,只需要TL_k_idx/= 255;你会得到你想要的。