I try search small images in big a image.But it does not always work correctly.For example I have two identical images but their quality is different.[C:\fakepath\Page-1.jpg](/upfiles/1454862946610774.jpg)[C:\fakepath\Page-2.jpg](/upfiles/14548632081292511.jpg)
How can I improve the search algorithm to each selected image corresponding values (coefficients).
Mat::ones(Size(12, 12), CV_8UC1);//clear a bit, I have change Depending on the image
dilate(bw, bw, kernel);
erode(bw, bw, kernel);
//ignore small contour ... may be is noise
if (contours[idx].size() < 40) continue;//40, I have change Depending on the image
Maybe I need another filter cleaning image or threshold.
void Main_ExtractStamps(char* name)
{
Mat img, src, dst, gray, bw;
img = imread(name);
src = img;
src.copyTo(dst);
//convert to gray scale
cvtColor(src, gray, CV_BGR2GRAY);
threshold(gray, bw, 0, 255, THRESH_BINARY | THRESH_OTSU);
gray = bw;
//Mat a;bilateralFilter(gray,a, 20, 1, 80);bw = a;
// clear a bit
Mat kernel = Mat::ones(Size(12, 12), CV_8UC1);//Change
dilate(bw, bw, kernel);
erode(bw, bw, kernel);
Mat copyMask(bw.size(), bw.type());
//detect outer contour of the stamps
vector> contours;
vector hierarchy;
vector stampsRect;
vector stampsRectRotated;
findContours(bw, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
// iterate through all the top-level contours,
int idx = 0;
for (; idx >= 0; idx = hierarchy[idx][0])
{
//ignore small contour ... may be is noise
if (contours[idx].size() < 40) continue; //Change
//store contour boundingRect
stampsRect.push_back(boundingRect(contours[idx]));
stampsRectRotated.push_back(minAreaRect(contours[idx]));
//prepare the copy mask
drawContours(copyMask, contours, idx, 255, CV_FILLED, 8, hierarchy);
//show selected contours
drawContours(dst, contours, idx, Scalar(0, 0, 255), 5, 8, hierarchy);
}
Mat rot_mat(2, 3, CV_32FC1);
RotatedRect rr;
for (idx = 0; idx < stampsRect.size(); idx++)
{
RotatedRect rr = stampsRectRotated[idx];
Rect rect = stampsRect[idx];
Point center(rect.width / 2, rect.height / 2);
//create an empty image with a default background
Scalar bkg(0, 0, 0);
Mat stamp(rect.height, rect.width, src.type(), bkg);
//copy the stamp from source using a mask
src(rect).copyTo(stamp, copyMask(rect));
//straighten
if (rr.angle != 0)
{
//rot_mat = getRotationMatrix2D(center, 90 + rr.angle, 1);
//warpAffine(stamp, stamp, rot_mat, stamp.size());
}
imwrite("../img/Stamp" + std::to_string(idx) + ".jpg", stamp);
imshow("Stamp" + to_string(idx), stamp);
}
imshow("Bin", bw);
imwrite("../img/StampDST..jpg", dst);
//imshow("DST", dst);
const char* pzOriginalImage = "Result Image";
namedWindow(pzOriginalImage, CV_WINDOW_AUTOSIZE);
resize(dst, dst, cvSize(1000, 800));
imshow(pzOriginalImage, dst);
waitKey();
}
Help me, I will be very thankful.
The result is not very good:

↧