I am trying to make a program to solve a puzzle an. My attempts work good with sample puzzles I made to test. Now I am trying to make one for a actual puzzle. The puzzle pieces of this new puzzle don't really have a proper shape.
I managed to make the image into black and white and finally in to array of 1s and 0s where 1s indicate the piece and 0s background. I want to find a way to identify the coordinates of the 4 corners, peaks and depths of these pieces.
I tried to count the numbers of 0s near the 1s to see the maximum curves in the border. But the shapes are not smooth enough for it to work.
counter = np.zeros((lenX,lenY),dtype=int)
for i in range(lenX):
for j in range(lenY):
if img[i,j]==1:
counter[i,j] = count_white(img,i,j,lenX,lenY)
print(counter)
tpath = os.getcwd()+"/test.jpg"
print(cv2.imwrite(tpath, Image))
print("saved at : ",tpath)
np.savetxt("test.csv", counter, delimiter=",")
def count_white(img,x,y,lenX,lenY):
X = [x-1,x,x+1,x+1,x+1,x,x-1,x-1]
Y = [y-1,y-1,y-1,y,y+1,y+1,y+1,y]
count = 0
for i in range(len(X)):
if X[i] < lenX and Y[i] < lenY:
if img[X[i],Y[i]] == 0:
count=count+1
return count
Any suggestion, references or ideas?
Sorry for the C++ code but it works for your case:
cv::Mat gray = cv::imread("Sq01a.png", cv::IMREAD_GRAYSCALE);
gray = 255 - gray;
cv::Mat bin;
cv::threshold(gray, bin, 1, 255, cv::THRESH_BINARY);
cv::Mat bigBin(2 * bin.rows, 2 * bin.cols, CV_8UC1, cv::Scalar(0));
bin.copyTo(cv::Mat(bigBin, cv::Rect(bin.cols / 2, bin.rows / 2, bin.cols, bin.rows)));
std::vector<std::vector<cv::Point> > contours;
cv::findContours(bigBin, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_NONE);
if (contours.size() > 0)
{
std::vector<cv::Point> tmp = contours[0];
const cv::Point* elementPoints[1] = { &tmp[0] };
int numberOfPoints = (int)tmp.size();
cv::fillPoly(bigBin, elementPoints, &numberOfPoints, 1, cv::Scalar(255, 255, 255), 8);
}
int maxCorners = 20;
double qualityLevel = 0.01;
double minDistance = bigBin.cols / 8;
int blockSize = 5;
bool useHarrisDetector = true;
double k = 0.04;
std::vector<cv::Point2f> corners;
cv::goodFeaturesToTrack(bigBin, corners, maxCorners, qualityLevel, minDistance, cv::noArray(), blockSize, useHarrisDetector, k);
std::vector<cv::Point2f> resCorners;
std::vector<cv::Point2f> imgCorners = { cv::Point2f(0, 0), cv::Point2f(bigBin.cols, 0), cv::Point2f(bigBin.cols, bigBin.rows), cv::Point2f(0, bigBin.rows) };
for (auto imgCorn : imgCorners)
{
size_t best_i = corners.size();
float min_dist = bigBin.cols * bigBin.rows;
for (size_t i = 0; i < corners.size(); ++i)
{
float dist = cv::norm(imgCorn - corners[i]);
if (dist < min_dist)
{
best_i = i;
min_dist = dist;
}
}
if (best_i != corners.size())
{
resCorners.push_back(corners[best_i]);
}
}
cv::Mat bigColor;
cv::cvtColor(bigBin, bigColor, cv::COLOR_GRAY2BGR);
for (auto corner : corners)
{
cv::circle(bigColor, corner, 10, cv::Scalar(0, 0, 255.), 1);
}
for (auto corner : resCorners)
{
cv::circle(bigColor, corner, 5, cv::Scalar(0, 255, 0), 2);
}
cv::imshow("gray", gray);
cv::imshow("bigColor", bigColor);
cv::waitKey(0);
Here red circles - corners from Harris and green - the nearest to the image corners. It's OK?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With