更新畸变矫正算法
This commit is contained in:
parent
4e1ba85154
commit
367d2ac4ab
|
@ -2,6 +2,8 @@
|
|||
|
||||
int CISTestImageProcess::test(const cv::Mat& image, CISTestResult& result)
|
||||
{
|
||||
|
||||
#if 0
|
||||
//std::vector<cv::RotatedRect> marks;
|
||||
//int res = findMarks(image, marks);
|
||||
//return res;
|
||||
|
@ -10,7 +12,9 @@ int CISTestImageProcess::test(const cv::Mat& image, CISTestResult& result)
|
|||
result.scaleXY3 = -1;
|
||||
|
||||
if (image.channels() != 3)
|
||||
return -1;
|
||||
return -1; //图片不是彩色图
|
||||
|
||||
int res = 0;
|
||||
|
||||
cv::Mat hsv;
|
||||
cv::cvtColor(image, hsv, cv::COLOR_BGR2HSV_FULL);
|
||||
|
@ -18,7 +22,7 @@ int CISTestImageProcess::test(const cv::Mat& image, CISTestResult& result)
|
|||
cv::split(hsv, hsv_channels);
|
||||
|
||||
cv::Mat mask_s, range_h, range_temp;
|
||||
cv::inRange(hsv_channels[1], 100, 255, mask_s); //饱和度在[50, 220]的像素
|
||||
cv::inRange(hsv_channels[1], 110, 255, mask_s); //饱和度在[50, 220]的像素
|
||||
|
||||
//cv::imwrite("range1.jpg", mask_s);
|
||||
cv::inRange(hsv_channels[0], 0, 84, range_h); //饱和度在[220, 255]的像素
|
||||
|
@ -26,16 +30,78 @@ int CISTestImageProcess::test(const cv::Mat& image, CISTestResult& result)
|
|||
range_h &= mask_s;
|
||||
range_h |= range_temp & mask_s;
|
||||
//cv::imwrite("range3.jpg", range_h);
|
||||
findEllipse(range_h, result.scaleXY1);
|
||||
cv::inRange(hsv_channels[0], 30, 150, range_h); //饱和度在[220, 255]的像素
|
||||
res = findEllipse(range_h, result.scaleXY1);
|
||||
if (res != 0)
|
||||
return -2;
|
||||
|
||||
cv::inRange(hsv_channels[0], 30, 140, range_h); //饱和度在[220, 255]的像素
|
||||
range_h &= mask_s;
|
||||
//cv::imwrite("range4.jpg", range_h);
|
||||
findEllipse(range_h, result.scaleXY2);
|
||||
res = findEllipse(range_h, result.scaleXY2);
|
||||
if (res != 0)
|
||||
return -2;
|
||||
|
||||
cv::inRange(hsv_channels[0], 128, 213, range_h); //饱和度在[220, 255]的像素
|
||||
range_h &= mask_s;
|
||||
//cv::imwrite("range5.jpg", range_h);
|
||||
findEllipse(range_h, result.scaleXY3);
|
||||
res = findEllipse(range_h, result.scaleXY3);
|
||||
if (res != 0)
|
||||
return -2;
|
||||
#else
|
||||
cv::Mat thre;
|
||||
if (image.channels() == 3)
|
||||
cv::cvtColor(image, thre, cv::COLOR_BGR2GRAY);
|
||||
else
|
||||
thre = image.clone();
|
||||
|
||||
cv::threshold(thre, thre, 127, 255, cv::THRESH_BINARY);
|
||||
|
||||
std::vector<std::vector<cv::Point>> contours;
|
||||
std::vector<cv::Vec4i> hierarchy;
|
||||
|
||||
findContours(thre, contours, hierarchy, cv::RETR_CCOMP, cv::CHAIN_APPROX_SIMPLE);
|
||||
|
||||
std::vector<cv::Point> contour;
|
||||
for (size_t i = 0; i < contours.size(); i++)
|
||||
{
|
||||
if (hierarchy[i][3] == -1)
|
||||
continue;
|
||||
|
||||
cv::Rect rect = cv::boundingRect(contours[i]);
|
||||
if (rect.width < 100)
|
||||
continue;
|
||||
else
|
||||
{
|
||||
contour = contours[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (contour.size() < 3)
|
||||
return -2;
|
||||
|
||||
cv::RotatedRect box = cv::fitEllipse(contour);
|
||||
|
||||
if (box.angle < -45)
|
||||
{
|
||||
box.angle += 90;
|
||||
float temp = box.size.width;
|
||||
box.size.width = box.size.height;
|
||||
box.size.height = temp;
|
||||
}
|
||||
if (box.angle > 45)
|
||||
{
|
||||
box.angle -= 90;
|
||||
float temp = box.size.width;
|
||||
box.size.width = box.size.height;
|
||||
box.size.height = temp;
|
||||
}
|
||||
float scale_xy = box.size.width / box.size.height;
|
||||
|
||||
result.scaleXY1 = scale_xy;
|
||||
result.scaleXY2 = scale_xy;
|
||||
result.scaleXY3 = scale_xy;
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -197,6 +263,9 @@ int CISTestImageProcess::findEllipse(const cv::Mat& image, double& scale_xy, dou
|
|||
|
||||
convexHull(maxContour, maxContour);
|
||||
|
||||
if (maxContour.size() < 3)
|
||||
return -1;
|
||||
|
||||
cv::RotatedRect box = cv::fitEllipse(maxContour);
|
||||
|
||||
if (box.angle < -45)
|
||||
|
|
Loading…
Reference in New Issue