color中只有蓝色的通道调整有用,红色和绿色怎么调都没有作用,不知道这是为什么?
#include <opencv2/opencv.hpp>
#include <string>
#include <iostream>
std::string Type2String(int type)
{
std::string strType;
uchar depth = type & CV_MAT_DEPTH_MASK;
uchar chans = 1 + (type >> CV_CN_SHIFT);
switch (depth)
{
case CV_8U:
strType = "CV_8U"; break;
case CV_8S:
strType = "CV_8S"; break;
case CV_16U:
strType = "CV_16U"; break;
case CV_16S:
strType = "CV_16S"; break;
case CV_32S:
strType = "CV_32S"; break;
case CV_32F:
strType = "CV_32F"; break;
case CV_64F:
strType = "CV_64F"; break;
default:
strType = "UNKNOWN_TYPE"; break;
}
strType += "C";
strType += (chans + '0');
return strType;
}
int main()
{
cv::Mat segmentation = cv::imread("D:/Desktop/轮廓test/segmentation.jpg");
if (segmentation.empty())
{
std::cout << "图片为空" << std::endl;
return 1;
}
std::cout << "segmentation:" << Type2String(segmentation.type()) << std::endl;
cv::Mat segmentation_gary;
cvtColor(segmentation, segmentation_gary, cv::COLOR_BGR2GRAY);
std::cout << "segmentation_gary:" << Type2String(segmentation_gary.type()) << std::endl;
std::vector< std::vector<cv::Point> > contours;
std::vector<cv::Vec4i> hierarchy;
findContours(segmentation_gary, contours, hierarchy, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE, cv::Point(0, 0));
std::cout << "segmentation:" << Type2String(segmentation.type()) << std::endl;
for (size_t i = 0; i < contours.size(); i++)
{
cv::Scalar color = (255, 0, 255);
drawContours(segmentation, contours, i, color, 5, 8, hierarchy, 0, cv::Point(0, 0));
}
imshow("轮廓", segmentation);
cv::waitKey();
return 0;
}
这是原图
这是运行结果(如果蓝色通道为0,其他通道为任何值都不显示线条)