Ask Your Question

Revision history [back]

I 've write this code in opencv but I think like @berak may be you can do it without opencv specialy online part. (I have supposed your image have 3 channels)

int main (int argc,char **argv) { Mat x = imread("C:/Users/Laurent.PC-LAURENT-VISI/Downloads/puzzle.png",CV_LOAD_IMAGE_ANYCOLOR); Mat img = imread("f:/lib/opencv/samples/data/lena.jpg",CV_LOAD_IMAGE_ANYCOLOR); x = x(Rect(0,0,512,512)); Mat xg,xgConnex,xgConnexuc,xgD; Mat stats,centroid; cvtColor(x,xg,COLOR_RGB2GRAY);

threshold(xg,xg,50,255,CV_THRESH_BINARY_INV); connectedComponentsWithStats(xg,xgConnex,stats,centroid,8,CV_16U); Mat kernel = Mat::ones(3,3,CV_16U); int nbIter=3; dilate(xgConnex, xgD,Mat() , Point(-1,-1),nbIter); cout << stats << "\n"; vector<mat> puzzle; puzzle.resize(stats.rows); for (int i = 0;i< puzzle.size(); i++) { puzzle[i] = Mat::zeros(stats.at<int>(i,3)+2nbIter,stats.at<int>(i,2)+2nbIter,img.type()); } // ONLINE for (int i = 0; i < img.rows; i++) { uchar p = img.ptr(i); ushort *s = (ushort)xgD.ptr(i); for (int j = 0; j < img.cols; j++, p += 3,s++) { int x=j - stats.at<int>(s, 0)+nbIter,y= i - stats.at<int>(s, 1)+nbIter; puzzle[s].at<vec3b>(y,x) = Vec3b(p, p[1], p[2]); } }

for (int i = 0;i< puzzle.size(); i++) { imshow(format("puzzle %d",i),puzzle[i]); } waitKey(); }

I 've write this code in opencv but I think like @berak may be you can do it without opencv specialy online part. (I have supposed your image have 3 channels)

int main (int argc,char **argv)
{
Mat x = imread("C:/Users/Laurent.PC-LAURENT-VISI/Downloads/puzzle.png",CV_LOAD_IMAGE_ANYCOLOR);
Mat img = imread("f:/lib/opencv/samples/data/lena.jpg",CV_LOAD_IMAGE_ANYCOLOR);
x = x(Rect(0,0,512,512));
Mat xg,xgConnex,xgConnexuc,xgD;
Mat stats,centroid;
cvtColor(x,xg,COLOR_RGB2GRAY);

cvtColor(x,xg,COLOR_RGB2GRAY); threshold(xg,xg,50,255,CV_THRESH_BINARY_INV); connectedComponentsWithStats(xg,xgConnex,stats,centroid,8,CV_16U); Mat kernel = Mat::ones(3,3,CV_16U); int nbIter=3; dilate(xgConnex, xgD,Mat() , Point(-1,-1),nbIter); cout << stats << "\n"; vector<mat> vector<Mat> puzzle; puzzle.resize(stats.rows); for (int i = 0;i< puzzle.size(); i++) { puzzle[i] = Mat::zeros(stats.at<int>(i,3)+2nbIter,stats.at<int>(i,2)+2nbIter,img.type()); Mat::zeros(stats.at<int>(i,3)+2*nbIter,stats.at<int>(i,2)+2*nbIter,img.type()); } // ONLINE for (int i = 0; i < img.rows; i++) { uchar p *p = img.ptr(i); ushort *s = (ushort)xgD.ptr(i); (ushort*)xgD.ptr(i); for (int j = 0; j < img.cols; j++, p += 3,s++) { int x=j - stats.at<int>(s, stats.at<int>(*s, 0)+nbIter,y= i - stats.at<int>(s, stats.at<int>(*s, 1)+nbIter; puzzle[s].at<vec3b>(y,x) = Vec3b(p, puzzle[*s].at<Vec3b>(y,x) = Vec3b(*p, p[1], p[2]); } }

} for (int i = 0;i< puzzle.size(); i++) { imshow(format("puzzle %d",i),puzzle[i]); } waitKey(); }

}