determine within cell the coordinates (rows/columns) having equal 3 numbers

2 visualizzazioni (ultimi 30 giorni)
How can you determine within cell 'out' the coordinates (rows/columns) having equal 3 numbers?
I would need to get a matrix that shows me the coordinates with 3 equal values.
rgb = randi(255,3,3,3,'uint8');
out = mat2cell(rgb,ones(1,size(rgb,1)),ones(1,size(rgb,2)),3);

Risposta accettata

dpb
dpb il 16 Ago 2023
[r,c]=find(cellfun(@(c)all(c==c(1)),out));
If there are none, the above will return [] empty results; taking off the find() will always return a logical array the size of out. What's more convenient will depend mostly on the use.

Più risposte (2)

Dyuman Joshi
Dyuman Joshi il 16 Ago 2023
You don't need to convert the data to cell array to find that -
rgb = randi(4,3,3,3,'uint8')
rgb = 3×3×3 uint8 array
rgb(:,:,1) = 4 4 1 3 1 2 3 3 4 rgb(:,:,2) = 4 4 1 3 3 3 1 2 4 rgb(:,:,3) = 2 1 1 4 1 2 1 3 2
%Method 1
[r,c]=find(rgb(:,:,2)==rgb(:,:,1) & rgb(:,:,3)==rgb(:,:,1))
r = 1
c = 3
%Method 2
[r,c]=find(all(rgb(:,:,2:3)==rgb(:,:,1),3))
r = 1
c = 3
Since the data is an unsinged integer, diff() will not be useful here, otherwise we can use this -
[r,c]=find(all(diff(rgb,1,3)==0,3));
The reason being - Subtracting a bigger number from a smaller number will result in a 0 for any unsigned integer, which will give incorrect result.
  11 Commenti
DGM
DGM il 17 Ago 2023
Modificato: DGM il 17 Ago 2023
I agree with @Dyuman Joshi here. This seems to all be part of a long composite thread about either selecting scattered pixels from a color image and getting a list of their color tuples, and it's morphed into a convoluted mess of seemingly unnecessary cell arrays.
Maybe it's all unrelated, and maybe it's hard to be certain from the outside perspective, but it sure smells like we're dealing with consequences of questionable decisions which need to be re-evaluated before we wind up digging a hole.
Dyuman Joshi
Dyuman Joshi il 18 Ago 2023
@Alberto Acri, Here's an approach -
rgb(:,:,1) =[4 4 1
3 1 2
1 2 4];
rgb(:,:,2) =[4 4 1
3 3 2
1 2 4];
rgb(:,:,3) =[4 1 1
4 1 2
1 2 2];
rgb = uint8(rgb);
%find the linear indices
linx = find(rgb(:,:,2)==rgb(:,:,1) & rgb(:,:,3)==rgb(:,:,1));
arr = rgb(:,:,1);
%values
val = arr(linx)
val = 5×1
4 1 2 1 2
%Get unique values and their indices
[vec, ~, ia] = unique(val, 'stable'); % Stable keeps it in the same order
%frequency of the unique values
bin = accumarray(ia, 1);
%Get indices of non-repeating values
[~,uniqueidx] = intersect(val,vec(bin<=1));
%Get the indices of repeating values by getting the difference
repeatidx = setdiff(1:numel(val),uniqueidx);
%Convert the corresponding repeating linear indices to subscripts
[r,c] = ind2sub(size(arr), linx(repeatidx))
r = 4×1
3 3 1 2
c = 4×1
1 2 3 3

Accedi per commentare.


C B
C B il 16 Ago 2023
I have taken dummy data instead of random inorder to get right co-ordinates, please let me know if you wanted to get same co-ordinates or different.
rgb = randi(255,3,3,3,'uint8')
rgb = 3×3×3 uint8 array
rgb(:,:,1) = 235 202 208 136 3 50 20 50 245 rgb(:,:,2) = 215 53 227 214 246 143 183 49 101 rgb(:,:,3) = 216 11 108 119 131 219 199 236 125
out = mat2cell(rgb,ones(1,size(rgb,1)),ones(1,size(rgb,2)),3);
rgb = uint8([...
100, 100, 100; 150, 150, 200; 255, 255, 255;
100, 150, 200; 200, 200, 200; 50, 50, 50;
255, 0, 255; 0, 255, 0; 150, 150, 150]);
rgb = reshape(rgb, [3, 3, 3])
rgb = 3×3×3 uint8 array
rgb(:,:,1) = 100 100 255 150 200 0 255 50 150 rgb(:,:,2) = 100 150 0 150 200 255 255 50 150 rgb(:,:,3) = 100 200 255 200 200 0 255 50 150
out = mat2cell(rgb, ones(1, size(rgb, 1)), ones(1, size(rgb, 2)), 3);
matForm = cellfun(@(x) numel(unique(x)), out);
[rows, cols] = find(matForm == 1);
equal_coords = [rows, cols];
disp(equal_coords)
1 1 3 1 2 2 3 2 3 3

Prodotti


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by