access image after cutting

asked 2015-12-14 05:26:52 -0600

ggeo gravatar image

updated 2015-12-14 06:31:40 -0600

I have a 600x600 image ( Image) and i am cutting into pieces of 200x200 (piecesImages),by creating 3x3 blocks (Height = 3 , Width =3).

Mat pieceImageFrame (200,200,CV_32F);
vector<Rect> piecesImagesRois;
vector<Mat> piecesImages;
piecesImagesRois.reserve( Width * Height );
piecesImages.reserve( Width * Height );

for( int i = 0; i <ImageSize.height; i+= pieceImageSize.height ) //ImageSize.height = 600
{
    for( int j = 0; j < ImageSize.width; j+= pieceImageSize.width )//ImageSize.width = 600
    {
        Rect myrect = Rect( j,i, pieceImageSize.width, pieceImageSize.height );
        pieceImageFrame = Image( myrect );
        piecesImages.push_back( pieceImageFrame.clone() );
        piecesImagesRois.push_back(myrect);             
    }
}

Now, I would expect to be able to access every image until :

piecesImages[ 0 ].at<float>( 199,199)

but as I can see I am able to access:

piecesImages[ 0 ].at<float>( 300,239999 )  !!

I would expect a grabage value but the values seem ok!

edit retag flag offensive close merge delete

Comments

look at my solution for such case http://answers.opencv.org/question/76210

sturkmen gravatar imagesturkmen ( 2015-12-14 05:38:48 -0600 )edit

@sturkmen:Thank's but I am trying to understand what is wrong with my code.

ggeo gravatar imageggeo ( 2015-12-14 05:47:52 -0600 )edit
1

Rect myrect = Rect( j,i, pieceImageSize.width, pieceImageSize.height ); pieceImageFrame = Image( rect );

Which is the variable rect? Because the defined rectangle is called myrect

LorenaGdL gravatar imageLorenaGdL ( 2015-12-14 05:59:59 -0600 )edit

Also, if I'm properly understanding your variables, this line:

for( int i = 0; i <ImageSize.height - Height; i+= pieceImageSize.height )

should be replaced with this (and same for the other loop):

for( int i = 0; i <ImageSize.height - pieceImageSize.height; i+= pieceImageSize.height )
LorenaGdL gravatar imageLorenaGdL ( 2015-12-14 06:06:00 -0600 )edit

@LorenaGdL:Hello ,I updated.I am scanning in the loop the whole image (600x600) and I am creating 9 pieces of 200x200. ( 800/200 = 3)

ggeo gravatar imageggeo ( 2015-12-14 06:30:58 -0600 )edit

Well, there's nothing wrong with your code now. I've tested, just in case, and every image in the vector piecesImages is of size 200x200 as expected

LorenaGdL gravatar imageLorenaGdL ( 2015-12-14 06:40:56 -0600 )edit

@LorenaGdL:That's right.But , why can I access at<float>( 300,239999 ) ??I would expect until at<float>( 199,199).Thanks

ggeo gravatar imageggeo ( 2015-12-14 06:51:58 -0600 )edit

I don't know, but maybe you're accessing the next Mat in the vector. Anyway, why do you care about it? If you're gonna use index, you better make sure you're in range. In fact, why do you even want to access a specific pixel that way? If you're gonna use a per-pixel loop later, avoid it...

LorenaGdL gravatar imageLorenaGdL ( 2015-12-14 06:57:48 -0600 )edit

@LorenaGdL:I just want to know why this happens. You said : f you're gonna use a per-pixel loop later, avoid it....And then ,what should I use?

ggeo gravatar imageggeo ( 2015-12-14 07:08:34 -0600 )edit

Well. it depends on what operation you're going to do, but most of them have overloaded operators. And in case you really really to loop, iterators are always a better option

LorenaGdL gravatar imageLorenaGdL ( 2015-12-14 07:10:42 -0600 )edit