Ask Your Question
1

Image shuffle not working

asked 2012-08-21 12:51:51 -0600

Pat gravatar image

updated 2012-08-22 10:12:58 -0600

I know for a fact that the image I receive in the arguments method is correct, because if I omit the shuffle I get the image displayed correctly.

When I use this function the resulting image is exactly the same, as if nothing had been done. I'm not sure if I am handling the ROI copies correctly :

void OpenCvHandler::shuffleImage(cv::Mat &image)
{
int topX = 0, topY = 0, randX = 0, randY = 0;
std::vector<Pair> pile;

while(pile.size() < (PUZZLE_TILE_NUMBER * PUZZLE_TILE_NUMBER))
{
    do
    {
        randX = PUZZLE_MASK_DIMS;
        randY = PUZZLE_MASK_DIMS;
        randX *= rand() % PUZZLE_TILE_NUMBER;
        randY *= rand() % PUZZLE_TILE_NUMBER;
    }
    while( pairWithinVector(pile, randX, randY) );

    cv::Mat src = image(cv::Rect(topX, topY, PUZZLE_MASK_DIMS, PUZZLE_MASK_DIMS));
    cv::Mat dst = image(cv::Rect(randX, randY, PUZZLE_MASK_DIMS, PUZZLE_MASK_DIMS));
    cv::Mat tmp;
    dst.copyTo(tmp);
    dst = src;
    src = tmp;

    pile.push_back(Pair(randX, randY));
    topX += PUZZLE_MASK_DIMS;

    if(topX == PUZZLE_DIMS)
    {
        topY += PUZZLE_MASK_DIMS;
        topX = 0;
    }
}
}

Here are the values of the constants I use :

static const int PUZZLE_TILE_NUMBER = 3;
static const int PUZZLE_MASK_DIMS = 260;
static const int PUZZLE_DIMS = PUZZLE_TILE_NUMBER * PUZZLE_MASK_DIMS;

Essentially with these values I copy tiles of 260x260 in different parts of the image and copy the destination image to where the copied image once was.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
4

answered 2012-08-23 06:20:31 -0600

Michael Burdinov gravatar image

I guess you think that 'dst = src' and 'src = tmp' operations perform copy of image. But in fact they are not. This is just change of pointer and reference counter. If you want to copy the image itself you should use copyTo() function like you did with tmp image.

edit flag offensive delete link more

Comments

Yes, I did that and it works now. I just assumed that if I use regular affectation of ROI openCV would only copy the region and not just the pointer. Thanks.

Pat gravatar imagePat ( 2012-08-24 09:46:54 -0600 )edit

There is also Mat::clone() method. e.g. Mat newImg = img.clone();

Baris Evrim Demiroz gravatar imageBaris Evrim Demiroz ( 2012-08-28 01:34:11 -0600 )edit

I don't think clone() method will work for this problem. The task is not to create copy of data, but to put it at specific place.

Michael Burdinov gravatar imageMichael Burdinov ( 2012-08-28 08:37:00 -0600 )edit

Question Tools

Stats

Asked: 2012-08-21 12:51:51 -0600

Seen: 558 times

Last updated: Aug 23 '12