Ask Your Question
0

load nearby pixels using mouse

asked 2014-10-10 14:59:54 -0500

Eng Hind gravatar image

updated 2014-10-10 15:08:45 -0500

Suppose i have an image and i am at a particular pixel with my mouse [say 0] and i want to take N*N of pixels around it like this:

'0' 1 2
3 4 5
6 7 8

I am trying to cycle through all pixels and am attempting to access 0,1,2, 3,5 6,7,8 and i want to store their values on new array
and if i move with my mouse to a new pixel for example pixel [1] the block will be :

'1' 2 3

4 5 6

7 8 9

and so on

and again i will store the new pixel and it's nearby pixels on the array .. could any one Plz help me how to do this with C++ openCV..

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2014-10-10 15:43:46 -0500

If you are at pixel x,y you could copy the rectangle into a new Mat as

cv::Mat foo = img.colRange(x-1,x+1).rowRange(y-1,y+1)
edit flag offensive delete link more

Comments

Thank you very much .. your answer was very helpful for me :)

Eng Hind gravatar imageEng Hind ( 2014-10-11 15:22:02 -0500 )edit

But now i have a problem with image Edges .. if the mouse on a pixel near one of image edges or corners i have an exception because the range of col & rows .. could you plz help me how to solve this problem ?

Eng Hind gravatar imageEng Hind ( 2014-10-14 09:44:58 -0500 )edit

just write it as img.colRange(max(x-1,0),min(x+1,img.cols) [or img.cols-1?]

FooBar gravatar imageFooBar ( 2014-10-17 08:19:15 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2014-10-10 14:59:54 -0500

Seen: 110 times

Last updated: Oct 10 '14