Ask Your Question
0

C++ OpenCv Randomly selecting the pixels having atleast 16 pixels space from one another

asked 2016-02-18 08:44:16 -0600

Manan gravatar image

Hi Guys,

I have currently an image having dimensions (2800 * 3800), I want to access the pixels randomly and store those pixel's in a vector. What Actually I want is that I should not be be storing or getting pixels which are unique and also these should have a space of 16 i.e consecutive 16 pixels difference between all pixels and Lastly I have to select atleast 300 Pixels.

Suggest me something really robust.

Thanks.

edit retag flag offensive close merge delete

Comments

If you want a vector of values that may be pixels values, then use a rng for filling it with values ((example)[http://docs.opencv.org/2.4/doc/tutorials/core/random_generator_and_text/random_generator_and_text.html])... What is the vector for?

thdrksdfthmn gravatar imagethdrksdfthmn ( 2016-02-18 09:47:56 -0600 )edit

@thdrksdfthmn okaye but I want to have combination of 300 pixels having gap of atleast 16 pixels. And If I generate random number for 3800 or 2800 i cant get unique pixels becasuse 300*16 is greater than both 3800 and 2800..!! any suggestion to have a combine Rows & Cols. could be utilize then we can have more unique combinations.

Manan gravatar imageManan ( 2016-02-18 10:23:02 -0600 )edit

Ok, what I meant is: a pixel is usually 0..255, so you have to generate a value between 0 and 255 and push it in the vector, when you have 16 (or how many you want) you do the thing with the vector, for example you put it in a cv::Mat. But I think I misunderstood... What you want is to have at least 300 positions that are not closer than 16 pixels one to another?

thdrksdfthmn gravatar imagethdrksdfthmn ( 2016-02-18 11:00:40 -0600 )edit

@thdrksdfthmn I want is to have at least 300 positions that are not closer than 16 pixels one to another. Also my image MAt has 2800 rows and 3800 columns.

Manan gravatar imageManan ( 2016-02-18 13:27:51 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2016-02-19 05:00:38 -0600

thdrksdfthmn gravatar image

updated 2016-02-19 08:59:20 -0600

Now that your question is clear, I can give you an answer:

cv::RNG rng(cv::getTickCount()); // for having each time different numbers
int lo = 16;
int hi = max(16, sqrt(image.cols * image.rows / 300) - 1); // maximum distance between points
std::vector< uchar > distantPixels;
cv::Point currentPos(0,0);
while (currentPos.y < image.rows)
{
    while (currentPos.x < image.cols)
    {
        distantPixel.push_back(image.ptr< uchar >(currentPos.y)[currentPos.x]);
        currentPos.x += rng.uniform(lo, hi);
    }
    currentPos.x = 0;
    currentPos.y += rng.uniform(lo, hi);
}

PS: I did not tested it, sorry if it has some bugs, but you get the idea ;)

edit flag offensive delete link more

Comments

Please do the remarks if there is any ;)

thdrksdfthmn gravatar imagethdrksdfthmn ( 2016-02-19 06:46:35 -0600 )edit

You should check if the pixel is in the image after you added the random number.

FooBar gravatar imageFooBar ( 2016-02-19 06:56:08 -0600 )edit

Now it will always start with the first pixel (0, 0) but you will never get outside the image borders

thdrksdfthmn gravatar imagethdrksdfthmn ( 2016-02-19 09:04:46 -0600 )edit
1

Every row of your pixels will start at zero. Why not initalize currentPos with random and also reset x to random(lo,hi) instead of zero?

FooBar gravatar imageFooBar ( 2016-02-19 10:10:15 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-02-18 08:44:16 -0600

Seen: 885 times

Last updated: Feb 19 '16