Reading rubik cube colors with Opencv
I wrote a C ++ program using OpenCV that can detect and highlight the edges of any object from a live video. But now I don't know how to extract the four corners of a cube from the many edges that show up in the video. So I am looking for some help here.
Here is the link of the article I am using as a guide for my project. http://www.cs.ubc.ca/~andrejk/525project/525report.pdf You can find the code for this article at the link below. This is written in Python. (I'm using C ++ and I don't know Python) http://www.cs.ubc.ca/~andrejk/525project/cubefinder.py
According to the article, the next step is to "segment the edge with an adaptive threshold." Which I really don't understand. And also I don't know how to extract the corners of the cube then.
A quick overview of the method I used is as follows. 1. Webcam login 2. Apply Laplacian filter 3. Apply Hough line transform.
I get the following result.
code
using namespace std;
using namespace cv;
Mat laplacianFilter(Mat image)
{
Mat hImage;
GaussianBlur(image,hImage,Size(3,3),0,0,BORDER_DEFAULT);
cvtColor(hImage,hImage,CV_RGB2GRAY);
Laplacian(hImage,hImage,CV_16SC1,3,1,0,BORDER_DEFAULT);
convertScaleAbs(hImage,hImage,1,0);
return hImage;
}
Mat hghTransform(Mat image, Mat &image2)
{
Mat lImage;
Canny(image,image,50,200,3);
cvtColor(image,lImage,CV_GRAY2BGR);
vector<Vec4i> lines;
HoughLinesP(image, lines, 1, CV_PI/180, 50, 50, 10 );
for( size_t i = 0; i < lines.size(); i++ )
{
Vec4i l = lines[i];
line( image2, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0,255,0), 3, CV_AA);
}
return lImage;
}
int main()
{
int c;
VideoCapture cap(0);
Mat image;
Mat image2;
namedWindow("hghtransform");
namedWindow("laplacianfilter");
namedWindow("cannyOutput");
while(1)
{
cap>>image;
cap>>image2;
//Output
imshow("laplacianfilter",laplacianFilter(image));
imshow("cannyOutput",hghTransform(laplacianFilter(image),image2));
imshow("hghtransform",image2);
c=waitKey(33);
if(c==27)
return 0;
}
return 0;
}
source to share
The adaptive threshold will give you a clear edge line that will allow you to get the 9 squares of the rubik side correctly.
You can see a decent comparison of global versus adaptive threshold here:
here: https://sites.google.com/site/qingzongtseng/adaptivethreshold
original image:
global threshold:
adaptive threshold:
To the corner, I'm not sure if it was stated in the document, but I would do something like:
==> search area, for example 1
, 2
, 3
, 4
for upper-left
, upper-right
, lower-left
and lower-right
, respectively.
==> with a pattern matching algorithm.
hope this helps.
Note: You may want to have a background with less noise. =)
source to share