How to use output number to display corresponding block of image?

5 vues (au cours des 30 derniers jours)
Franziska Kögl
Franziska Kögl le 28 Avr 2021
Hello,
I have this code below, that gives me a 4x4 grid over an image and the location of the point (110,177) within it. In this particular case idx=8, because the point is located in the 8th cell when top left cell is 1 and you count from left to right and top to bottom. The output of idx is 1x1 double.
I also used mat2cell to split the image into 4x4 blocks named C1-C16.
Now I want a code that takes the location of the point (in this case 8) and then shows me the corresponding block of the image with imshow(C8) but I can´t figure out how to do this.
nx = 4;
ny = 4;
side = 224;
xedge = linspace(0,side,nx+1);
yedge = linspace(0,side,ny+1);
xpt=110
ypt=177
xbin = discretize(xpt, xedge);
ybin = discretize(ypt, yedge);
idx = sub2ind([nx, ny], xbin, ybin);

Réponses (1)

Raghava S N
Raghava S N le 26 Avr 2024
Hi,
From my understanding of the post, there exists an image, and the goal is to find a specific point. The image is divided into the blocks of a 4x4 grid, and the required functionality must be able to directly index the block in which the point exists and then show that block. This can be done by storing the 16 blocks of the 4x4 grid in a linearised “cell array” which can then be indexed to extract the required block.
The image can be split into blocks of 4x4 and stored in a “cell array” using the “mat2cell” function. Please follow the MATLAB example code given below for more understanding:
% Assuming img stores your image
imgSize = 224;
nx = 4;
ny = 4;
blockSizeRow = imgSize / ny;
blockSizeColumn = imgSize / nx;
% Use mat2cell to split the image into 4x4 blocks
C2D = mat2cell(img, repmat(blockSizeRow, [1, ny]), repmat(blockSizeColumn, [1, nx]), 3)';
%The last parameter 3 is required for RGB images; can be omitted for grayscale.
%Store the transposed value of the expression in C2D as “mat2cell” splits the
%image down the row and then across the column, which is the opposite of the functionality desired.
% Convert the 2D cell array to a linearized (1D) cell array
C = reshape(C2D, [1, nx*ny]);
% Now, C{idx} will give the desired block directly
After this, the index is calculated with the code provided in the post and stored in the variable “idx”. On running the code provided in the post, the value for “idx” is “14” and not “8” as mentioned in the post. This is because in the code, the “x” axis is right-ward and “y” axis is down-ward. If “idx” must store “8” on executing the code, the axes must be flipped. Therefore, “xpt” should be 177 and “ypt” must be 110.
The required block can then be displayed using the MATLAB code given below:
% Access and show the block containing the point using linearized “cell array”
selectedBlock = C{idx}; % Direct access using idx
figure; % Open a new figure window
imshow(selectedBlock);
title(sprintf('Block Containing the Point (%d,%d)', xpt, ypt));
Refer to this MATLAB R2024a documentation link for more information on “mat2cell”- https://www.mathworks.com/help/matlab/ref/mat2cell.html.
For more information on the MATLAB “cell array” type, refer to this link MATLAB R2024a documentation link- https://www.mathworks.com/help/matlab/cell-arrays.html?searchHighlight=cellarray&s_tid=srchtitle_support_results_1_cellarray.
Hope this helps.

Catégories

En savoir plus sur Computer Vision with Simulink dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by