How I can repeatedly divide a binary image ?

I want to divide a binary image into 4 equal parts first. Then each parts again to 4 parts and so on. I want to repeat this work upto certain no of times . How to do that?

Réponses (3)

darova
darova le 13 Avr 2019
Use mat2cell()
clc,clear
A = imread('fig3.png');
I = im2bw(A);
k = 128; % divide matrix into 128 parts
[m,n] = size(I);
rows_rest = mod(m,k);
cols_rest = mod(n,k);
nrows = (m-rows_rest)/k *ones(1,k);
ncols = (n-cols_rest)/k *ones(1,k);
nrows(1:rows_rest) = nrows(1:rows_rest) + 1;
ncols(1:cols_rest) = ncols(1:cols_rest) + 1;
C = mat2cell(I,nrows,ncols);

7 commentaires

Zara Khan
Zara Khan le 13 Avr 2019
You are diving the whole image into 128 blocks that I don't want. I want to di this work step by step. First want to divide the image into 4 equal parts then each part to again 4 parts and so on.
darova
darova le 13 Avr 2019
Why cant you rebuild that code with loops?
Zara Khan
Zara Khan le 13 Avr 2019
No
Image Analyst
Image Analyst le 13 Avr 2019
You CAN do this with loops if you want. You can maybe do it with other ways like reshape or permute or something -- I'd have to spend time to look into it.
Would you want all the chunks of this divided-up image to be stacked as slices in a 3D array - this is more efficient than a cell array?
Zara Khan
Zara Khan le 13 Avr 2019
Actually after dividing each image I want calculate the no of white pixels of each parts then will store it in a excel sheet. I am working with 1000 dataset. So it will be 1000x no of cols
Zara Khan
Zara Khan le 14 Avr 2019
Image Analyst: what will be the easiest way ??
darova
darova le 14 Avr 2019
The best way is to pay someone

Connectez-vous pour commenter.

Image Analyst
Image Analyst le 14 Avr 2019
Your image is not a power of 2.
Try this code:
clc; % Clear the command window.
clear all;
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
%===============================================================================
% Get the name of the image the user wants to use.
baseFileName = 'fig3.png';
folder = pwd;
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
%=======================================================================================
% Read in demo image.
grayImage = imread(fullFileName);
% Get the dimensions of the image.
[rows, columns, numberOfColorChannels] = size(grayImage)
if numberOfColorChannels > 1
grayImage = grayImage(:, :, 3);
end
% Make sure it's square
if rows ~= columns
grayImage = imresize(grayImage, [rows, rows]);
% Get the dimensions of the image.
[rows, columns, numberOfColorChannels] = size(grayImage)
end
% Display image.
subplot(2, 2, 1);
imshow(grayImage, []);
impixelinfo;
axis('on', 'image');
caption = sprintf('Original image: %d rows by %d columns', rows, columns);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0.05 1 0.95]);
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
% Define the function that we will apply to each block.
% First in this demo we will take the median gray value in the block
% and create an equal size block where all pixels have the median value.
% Image will be the same size since we are using ones() and so for each block
% there will be a block of 8 by 8 output pixels.
nnzFunction = @(theBlockStructure) nnz(theBlockStructure.data(:));
% Now do a loop to divide by half each time and count the number of non-zeros.
counter = 1;
newRows = rows;
newCols = columns;
while newRows >= 1
% Round sizes.
newRows = floor(newRows);
newCols = floor(newCols);
% Resize the image.
grayImage = imresize(grayImage, [newRows, newCols], 'nearest');
% Get the dimensions of the image.
[rows, columns, numberOfColorChannels] = size(grayImage)
% Show the image
subplot(2, 2, 2);
imshow(grayImage);
axis('on', 'image');
caption = sprintf('%d rows by %d columns', newRows, newCols);
title(caption, 'FontSize', fontSize);
% Count the number of non-zero pixels.
whitePixelCount(counter) = nnz(grayImage);
% Display counts so far.
subplot(2, 2, 3:4);
plot(whitePixelCount, 'b.-', 'MarkerSize', 30);
grid on;
title('White Pixel Count', 'FontSize', fontSize);
xlabel('Image number', 'FontSize', fontSize);
ylabel('Count (Number of White Pixels)', 'FontSize', fontSize);
drawnow;
% Compute half the number of rows and columns.
% May be a fractional number if the rows and columns are not a power of 2.
newRows = (rows / 2);
newCols = (columns / 2);
% Show user the results.
promptMessage = sprintf('%d white pixels in this image.\nDo you want to Continue processing,\nor Quit processing?', whitePixelCount(counter));
titleBarCaption = 'Continue?';
buttonText = questdlg(promptMessage, titleBarCaption, 'Continue', 'Quit', 'Continue');
if contains(buttonText, 'Quit')
return;
end
% Increment index for whitePixelCount array.
counter = counter + 1;
end
% Show in command window.
whitePixelCount
0001 Screenshot.png

4 commentaires

Zara Khan
Zara Khan le 16 Avr 2019
Modifié(e) : Zara Khan le 16 Avr 2019
Image Analyst:
You are always making two equal parts of this image. But at every steps I want to get 4 equal parts as long as we can get. Please can you give me any hints in this case.
I am using this :
q1=image(1:size(image,1)/2,1:size(image,2)/2,:);
q2=image(size(image,1)/2+1:size(image,1),1:size(image,2)/2,:);
q3=image(1:size(image,1)/2,size(image,2)/2+1:size(image,2),:);
q4=image(size(image,1)/2+1:size(image,1),size(image,2)/2+1:size(image,2),:);
But its becoming difficult when repeating the process at every steps, too many variables I need to handle. How can I loop over the process ?
Image Analyst
Image Analyst le 18 Avr 2019
Start with a small image of 16 by 16 pixels and explain step by step what you want as the output at each step.
Zara Khan
Zara Khan le 19 Avr 2019
I want to set a label like :
Label 1: 16X16 will be divided into 4 equal quadrants ,now will store number of white pixels from 4 quadrants
Label 2: Each 8X8 will be divided into 4 equal quadrants again , now will store number of white pixels from 4(previous) + 32(now)
This will continue as many labels we want.
note: I am taking the complement of the 'fig3.png'

Connectez-vous pour commenter.

Walter Roberson
Walter Roberson le 19 Avr 2019

0 votes

7 commentaires

Zara Khan
Zara Khan le 19 Avr 2019
I want label here. I want to provide labels and want to store number if white pixels like I mentioned
I want to set a label like : Label 1: 16X16 will be divided into 4 equal quadrants ,now will store number of white pixels from 4 quadrants Label 2: Each 8X8 will be divided into 4 equal quadrants again , now will store number of white pixels from 4(previous) + 32(now) This will continue as many labels we want. note: I am taking the complement of the 'fig3.png'
What does it mean to "set a label" ?
Zara Khan
Zara Khan le 19 Avr 2019
Modifié(e) : Zara Khan le 19 Avr 2019
I mean I want define how long I want to divide the image. But at every step we will get 4 quadrants
Image Analyst
Image Analyst le 19 Avr 2019
Do you know how to do recursive functions?
function splitted = rsplit4(img, sd)
[r, c, p] = size(img);
if sd <= 0 || mod(r,2) || mod(c,2)
splitted = img;
else
splitted = {rsplit4(img(1:end/2,1:end/2, :), sd-1), rsplit4(img(1:end/2,end/2+1:end, :), sd-1);
rsplit4(img(end/2+1:end,1:end/2,:), sd-1), rsplit4(img(end/2+1:end,end/2+1:end,:)), sd-1};
end
end
Now you call rsplit4 passing in the image and the maximum number of subdivisions.
Zara Khan
Zara Khan le 20 Avr 2019
can I store white pixels counts from each blocks gradully in an array format?
Like said
step 1: 16X16 will be divided into 4 equal quadrants ,now will store number of white pixels from 4 quadrants
step 2: Each 8X8 will be divided into 4 equal quadrants again , now will store number of white pixels from 4(previous) + 32(now)
For one image it will be 1X32
for 10 image ---10X32 in this format.
white pixels count from 32 blocks .
Zara Khan
Zara Khan le 23 Avr 2019
Image Analyst : No

Connectez-vous pour commenter.

Question posée :

le 13 Avr 2019

Commenté :

le 23 Avr 2019

Community Treasure Hunt

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

Start Hunting!

Translated by