效果
OK的:
NG的:
代码
#define show(image) cv::namedWindow(#image,cv::WINDOW_NORMAL);\
cv::imshow(#image, image)
int i = 1;
while(1)
{
cv::Mat src;
QString dir("D:\\QtProject\\Opencv_Example\\solar_cell\\solar_cell_");
QString path;
if(i>9)
path = QString("%1%2%3").arg(dir).arg(i++).arg(".png");
else
path = QString("%1%2%3%4").arg(dir).arg("0").arg(i++).arg(".png");
cout<<path.toStdString()<<endl;
src = cv::imread(path.toStdString(), cv::IMREAD_COLOR);
if (src.empty()) {
cout << "Cannot load image" << endl;
return;
}
show(src);
cout<<__func__<<src.channels();
cv::Mat rgbImages[3];
cv::split(src,rgbImages);
// cv::imshow("r",rgbImages[0]);
// cv::imshow("g",rgbImages[1]);
show(rgbImages[2]);
cv::Mat bImageNot;
cv::bitwise_not(rgbImages[2], bImageNot);
show(bImageNot);
cv::Mat ImageBinary;
cv::threshold(bImageNot, ImageBinary, 159, 255, cv::THRESH_BINARY);
show(ImageBinary);
std::vector<std::vector<cv::Point> > contoursDefect;
std::vector<cv::Vec4i> hierarchyDefect;
cv::Mat regionOut = cv::Mat::zeros(ImageBinary.size(), ImageBinary.type());
cv::Mat regionIn = cv::Mat::zeros(ImageBinary.size(), ImageBinary.type());
cv::Mat goodSmallRegion = cv::Mat::zeros(ImageBinary.size(), ImageBinary.type());
cv::Mat goodLongRegion = cv::Mat::zeros(ImageBinary.size(), ImageBinary.type());
//cv::cvtColor(ImageBinary, canves,cv::COLOR_RGBA2RGB);
cv::findContours(ImageBinary, contoursDefect, hierarchyDefect, cv::RETR_TREE, cv::CHAIN_APPROX_NONE);
for (size_t i = 0; i < contoursDefect.size(); i++)
{
cv::Mat contour(contoursDefect.at(i));//第i个轮廓
double area = contourArea(contour);
cv::RotatedRect minRect = cv::minAreaRect(contour);
cv::Rect rect = minRect.boundingRect2f();
if (area >= 3500 && area <= 5000 && rect.width >= 130 && rect.width <= 300)
{
cv::drawContours(goodSmallRegion, contoursDefect, i, cv::Scalar(255), -1);
}
if (area >= 6000 && area <= 10000 && rect.width >= 350 && rect.width <= 550)
{
cv::drawContours(goodLongRegion, contoursDefect, i, cv::Scalar(255), -1);
}
if(hierarchyDefect[i][3] == -1 && area > 15000)
{
cv::drawContours(regionOut, contoursDefect, i, cv::Scalar(255), -1);
}
if(hierarchyDefect[i][3] != -1 && area > 15000)
{
cv::drawContours(regionIn, contoursDefect, i, cv::Scalar(255), -1);
}
}
show(goodSmallRegion);
show(goodLongRegion);
show(regionOut);
show(regionIn);
cv::Mat result;
void morphologyOperate(goodSmallRegion, goodLongRegion, regionOut, regionIn, result);
}