//分割颜色通道 vector<Mat> imgChannels; split(srcImage,imgChannels); //获得目标颜色图像的二值图 #ifdef RED Mat midImage=imgChannels.at(2)-imgChannels.at(0); #endif #ifndef RED Mat midImage=imgChannels.at(0)-imgChannels.at(2); #endif
这里说一下在 opencv c++ 中 Mat 之间是可以直接用 - 号相减的,而且如果减完后对应的像素点是负值的话会设为0,而在python中用 - 减完后如果像素值为负不会归零,而是类似于溢出的那种效果,也就是会变成255加上那个负数。在 c++ 中不会出现这样的问题。
//膨胀 int structElementSize=2; Mat element=getStructuringElement(MORPH_RECT,Size(2*structElementSize+1,2*structElementSize+1),Point(structElementSize,structElementSize));
hierarchy Optional output vector (e.g. std::vector<cv::Vec4i>), containing information about the image topology. It has as many elements as the number of contours.
For each i-th contour contours[i], the elements hierarchy[i][0] , hierarchy[i][1] , hierarchy[i][2] , and hierarchy[i][3] are set to 0-based indices in contours of the next and previous contours at the same hierarchical level, the first child contour and the parent contour, respectively.
If for the contour i there are no next, previous,parent, or nested contours, the corresponding elements of hierarchy[i] will be negative. ``` 所以遍历轮廓的语句会写成 `for(int i=0 ; i>=0 ; i=hierarchy2[i][0])` `hierarchy` 的大小和 `contours` 的大小一样,所以若其大小为零说明没有轮廓也就不能遍历了,遍历会报错。 ```cpp //找出轮廓的最小外接矩形 rect_tmp2=minAreaRect(contours2[i]); Point2fP[4]; //将矩形的四个点保存在P中 rect_tmp2.points(P);
dstRect[0]=Point2f(0,0); dstRect[1]=Point2f(width,0); dstRect[2]=Point2f(width,height); //透视变换,矫正成规则矩形 Mat warp_mat=getAffineTransform(srcRect,dstRect); Mat warp_dst_map;