here's how houghLinesP would look like. Not a solution yet, but maybe you can work with that:
int main()
{
cv::Mat image = cv::imread("SharpenCorners.jpg");
cv::imshow("image", image);
cv::Mat gray;
cv::cvtColor(image,gray,CV_BGR2GRAY);
cv::imshow("gray", gray);
cv::Mat output; image.copyTo(output);
// gradient:
cv::Mat gx, gy;
cv::Sobel(gray, gx, CV_32F, 1, 0, 3 );
cv::Sobel(gray, gy, CV_32F, 0, 1, 3 );
cv::Mat gm;
cv::magnitude(gx,gy,gm);
//cv::Mat g_thres = gray < 100;
cv::Mat g_thres = gm > 100;
//int lineThres = 5; // 1st image
//int lineThres = 10;// 2nd image
//int lineThres = 25;// 3rd image
int lineThres = 50;// 4th image
std::vector<cv::Vec4i> lines;
cv::HoughLinesP( g_thres, lines, 1, CV_PI/(4*180.0), lineThres, 10, 10 );
for( size_t i = 0; i < lines.size(); i++ )
{
cv::line( output, cv::Point(lines[i][0], lines[i][1]),
cv::Point(lines[i][2], lines[i][3]), cv::Scalar(155,255,155), 2, 8 );
}
cv::imwrite("houghpSharpen4.png", output);
cv::imshow("g thres", g_thres);
cv::namedWindow("output"); cv::imshow("output", output);
cv::waitKey(-1);
return 0;
}
with these results for given thresholds:
1st:
2nd:
3rd:
4th:
Additional idea:
Giving your own comment, that you might try to approximate each contour with rectangles, you could start with houghLinesP results (with a pretty high threshold to get at least all "long" line elements) and try to create the rectangles based on those lines