access image after cutting
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!
look at my solution for such case http://answers.opencv.org/question/76210
@sturkmen:Thank's but I am trying to understand what is wrong with my code.
Rect myrect = Rect( j,i, pieceImageSize.width, pieceImageSize.height ); pieceImageFrame = Image( rect );
Which is the variable
rect
? Because the defined rectangle is calledmyrect
Also, if I'm properly understanding your variables, this line:
should be replaced with this (and same for the other loop):
@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)
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:That's right.But , why can I access
at<float>( 300,239999 )
??I would expect untilat<float>( 199,199)
.ThanksI 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: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?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