how to get random pixel index from binary image with value 1 ?

2 ビュー (過去 30 日間)
bamini thavarajah
bamini thavarajah 2017 年 12 月 4 日
コメント済み: Walter Roberson 2018 年 9 月 16 日
I have a binary image of large size (256x256). In this image most of the pixel values are zero and some of them are 1. I need to get only 100 randomly chosen pixel coordinates with value 1 from image.

回答 (1 件)

Guillaume
Guillaume 2017 年 12 月 4 日
pixels = [ones(1, 100), zeros(1, 256*256-100)];
img = reshape(pixels(randperm(numel(pixels))), 256, 256);
  2 件のコメント
Ahlam Benahmeida
Ahlam Benahmeida 2018 年 9 月 16 日
can you help me please, if I have a color image and I want to extract the random pixels value and save them in a new matrix?
Walter Roberson
Walter Roberson 2018 年 9 月 16 日
num_needed = 100;
[rows, cols, panes] = size(YourImage);
selected_indices = randperm(rows * cols, num_needed);
[r, c] = ind2sub([rows, cols], selected_indices(:));
random_pixel_r = YourImage(r, c, ones(num_needed,1));
random_pixel_g = YourImage(r, c, 2*ones(num_needed,1));
random_pixel_b = YourImage(r, c, 3*ones(num_needed,1));
random_new_image = cat(3, random_pixel_r, random_pixel_g, random_pixel_b);

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeImages についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by