You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
how to find the matrix of 0's and 1's of the binary image in matlab
5 views (last 30 days)
Show older comments
faraz.a
on 26 May 2013
hello i need a code in matlab and C or C++ and java to upload a binary image and then find its pixel values and write it in the form of matrix of 0's and 1's. Then i need to check every pixel and few of it's neighbors.
Answers (1)
Image Analyst
on 26 May 2013
We can help you with MATLAB code, but usually not with C, C++, or Java. Where do you want to upload this binary image to? You forgot to tell us its destination. Is it some web site? Have you seen urlwrite?
urlwrite(URL,filename) readsWeb content at the specified URL and saves it tothe file specified by filename.
urlwrite(URL,filename,Name,Value)uses additional options specified by one or more Name,Value pairarguments.
Where do you want to write the image to? A file? The command window? A GUI? You forgot to say where you want to write it to.
What does "find its pixel values" and "check every pixel" mean to you? An image is an array of numbers so to "check it" you just give the subscripts, like pixelValue = imageArray(row, column) where row and column are numbers.
8 Comments
faraz.a
on 26 May 2013
I want to upload an binary image or a gray image from my laptop. I have a question were it says find the MSB and LSB and we can find the only after getting the pixel values. which rages from 0-255 i guess. so how to get that pixel values and if use the subscript pixelValue = imageArray(row, column)it gives me the image pixel values in array like "011100011110011001" write? i want to write it in a form of matrix. this is the question Consider an image in which each pixel value is represented by k bits. By selecting a single bit from the same position in the binary representation of each pixel, a binary image called a bit plane can be formed. (a) Extract the most significant bit plane (MSB) and the least significant bit plane (LSB) of the Boy image and print the binary image of each bit plane (some image is given) Consider the 4‐pixel neighborhood ABCD as C B D A X Write a computer program to find the entropy of the binary image by modeling it as (b) DMS source (no conditioning contexts) (c) Markov source conditioned over the previous pixel labeled A (2 contexts) (d) Markov source conditioned over the 2 neighboring pixels A and B (4 contexts) (e) Markov source conditioned over the 3 neighboring pixels A, B, C (8 contexts) (f) Markov source conditioned over the 4 neighboring pixels A, B, C, D (16 contexts) Note that if the image is predictable (such as the MSB image), the entropy of the model should decrease as you add more contexts until you capture all the correlation in the image. When you are processing the pixels in the first row or the first column of the image, a minor problem arises in forming some context models because some of the neighboring pixels might fall outside the image. A simple way of avoiding this issue is to skip the first row of the image (assume that those values have already been communicated to the decoder by other means) and to also skip the first pixel of each line. Aside from that, if any of the pixels ABCD fall outside the image (this might happen for some contexts when X falls on the last column), build the context by replicating the last column pixels in the vertical direction. Use the number of times that you visit each context divided by the total number of pixels that you visit as the steady state probability of that context.
I am unable to understand how to check the pixel X and its neighbors everytime and count it and store the number of 0's and 1's " first i need to find the matrix of the image in 0's and 1's then i need to check the pixels whether it is 0 or 1.. i need to check all the pixels and simultaneously its neighbors and if it's neighbor hood are "0000" and the pixel X is 0 i need to add context(0 0 0 0) = (1+n)/(total number of pixels or total number of times we checked the pixels)). I can write algorithm and send you and how to send an image to you guys? i nee to show what i actually need
Image Analyst
on 26 May 2013
Edited: Image Analyst
on 26 May 2013
"I can write algorithm and send you and how to send an image to you guys?" Post your image to http://snag.gy and your code here.
Here is a demo to extract and view bitplanes in a gray scale image:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
button = menu('Use which demo image?', 'CameraMan', 'Moon', 'Eight', 'Coins', 'Pout');
if button == 1
baseFileName = 'cameraman.tif';
elseif button == 2
baseFileName = 'moon.tif';
elseif button == 3
baseFileName = 'eight.tif';
elseif button == 4
baseFileName = 'coins.png';
else
baseFileName = 'pout.tif';
end
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize'));
% Let's compute and display the histogram.
[pixelCount grayLevels] = imhist(grayImage);
subplot(2, 2, 2);
bar(pixelCount);
grid on;
title('Histogram of Original Grayscale Image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
% Let's use this spot for the output image.
subplot(2, 3, 5);
for bp = 0 : 7
% Compute the bit plane image.
bitPlaneImage = bitand(grayImage, 2^bp);
% Display it as pure black and white (not gray scale).
imshow(bitPlaneImage, []);
caption = sprintf('You are now viewing bitplane %d.', bp);
title(caption, 'FontSize', fontSize);
% If it's not the last bit plane, ask them if they want to continue.
if bp ~= 7
message = sprintf('%s\nDo you want to continue', caption);
button = questdlg(message, 'Continue?', 'Yes', 'No', 'Yes');
drawnow; % Refresh screen to get rid of dialog box remnants.
if strcmpi(button, 'No')
break;
end
end
end
msgbox('Done with demo');
faraz.a
on 27 May 2013
My first question is (a)Extract the most significant bit plane (MSB) and the least significant bit plane (LSB) of the Boy image and print the binary image of each bit plane is this code right for question (a) clc; % Clear the command window. close all; % Close all figures (except those of imtool.) imtool close all; % Close all imtool figures. clear; % Erase all existing variables. workspace; % Make sure the workspace panel is showing. fontSize = 20; A=imread('coins.png');
B=bitget(A,1); figure, subplot(2,2,1);imshow(logical(B));title('Bit plane 1');
B=bitget(A,2); subplot(2,2,2);imshow(logical(B));title('Bit plane 2');
B=bitget(A,3); subplot(2,2,3);imshow(logical(B));title('Bit plane 3');
B=bitget(A,4); subplot(2,2,4);imshow(logical(B));title('Bit plane 4');
B=bitget(A,5); figure, subplot(2,2,1);imshow(logical(B));title('Bit plane 5');
B=bitget(A,6); subplot(2,2,2);imshow(logical(B));title('Bit plane 6');
B=bitget(A,7); subplot(2,2,3);imshow(logical(B));title('Bit plane 7');
B=bitget(A,8); subplot(2,2,4);imshow(logical(B));title('Bit plane 8');
now i want to save these bit planes some where in matlab or in my computer so that i can use it for the next part of the question I know that now these bit planes are binary images and it will consists of 0's and 1's only.. now i want to find the matrices form (which will obviously consist of 0's and 1's) of these bit planes and save them with some names bp7,bp6...
now coming to part (b) DMS source (no conditioning contexts)
what i want is i want to read one of the matrix(the one which we created from the bit planes) say bp7 so that i can count the number of 0's and 1's and calculate the probability of 0's and 1's (say it is a 256 * 256 matrix in that 200 are 0 and 56 are are 1's) so the probabilit should be of 0 should be 200/(total no. of pixels) and for 1 probability should be 56/(total no. of pixels))
say P0= probability of 0 P1 probability of 1
now need to find entropy which is summation of p0log(base2)(1/p1) + P1log(base2)(1/p1)
(or can i directly find entropies of those bit plane images? only for this question)
I am sending some images 1st the boy.tif image which is the original image from which i need to find msb and lsb bitplanes 2nd i am sending you and image of context modelling which explains why i need to have those matrix
there are few more problems i need to solve i am sending the image just tell me how to do that process in the bit plane images we created for the 1st question
(c) Markov source conditioned over the previous pixel labeled A (2 contexts)
(d) Markov source conditioned over the 2 neighboring pixels A and B (4 contexts)
(e) Markov source conditioned over the 3 neighboring pixels A, B, C (8 contexts)
(f) Markov source conditioned over the 4 neighboring pixels A, B, C, D (16 contexts)
Note that if the image is predictable (such as the MSB image), the entropy of the model should decrease as you add more contexts until you capture all the correlation in the image. When you are processing the pixels in the first row or the first column of the image, a minor problem arises in forming some context models because some of the neighboring pixels might fall outside the image. A simple way of avoiding this issue is to skip the first row of the image (assume that those values have already been communicated to the decoder by other means) and to also skip the first pixel of each line. Aside from that, if any of the pixels ABCD fall outside the image (this might happen for some contexts when X falls on the last column), build the context by replicating the last column pixels in the vertical direction. Use the number of times that you visit each context divided by the total number of pixels that you visit as the steady
faraz.a
on 27 May 2013
this is what i have to do with those "bit plane images" created in the question 1. but i am unable to understand can we do this without getting the matrix? if so how
Image Analyst
on 27 May 2013
I believe you can get that by filtering the image with [1,1,0;1,0,0;0,0,0]
contextImage = imfilter(binaryImage, [1,1,0;1,0,0;0,0,0]);
The value of contextImage at each location will be the sum of the 1's in the upper left L-shape by the pixel.
faraz.a
on 28 May 2013
what i want to do is step 1 read the image(any bit plane image created in (a) then write it in a 2-d array(matrix) assign i=1 and j=1 (1=rows, j= columns) assign A(i,j)=X (this is array) A(i,j-1)=A A(i-1,j)=B now give context=AB (which should be 00,01,10,11 any one of them) if { context=00 { if X=0 { c0(00)=1+c0(00) } else if X=1 { c1(00)=1+c1(00) (at start c1(00) c0(00) all are equal to zero) } } else if context=01 { if x= 0 { c0(01)=1+c0(01) } else if X=1 { c1(01)=1+c1(01) } cont(01)=1+cont(01) } else if context=10 {if X=0 { c0(10)=1+c0(10) } else if X=1 { if true % code c1(10)=1+c1(10) } cont(10)=1+cont(10) } else if context=11 { if X=0 { c0(11)=1+c0(11) } else if X=1 { c1(11)=1+c(11) } cont(11)=1+cont(11) } if j<256 (size of the image is 256*256 then we should take 256) {j=j+1 } else if j=256 { i=i+1
} if i=256 && j=256 (both should 25) end else go to { assign A(i,j)=X (it should go back to this) } then i have to find out some float values when displaying this it should c0me steady state probabilities p(00)=cont(00)/(total number of counts i think it will be the size of the matrix 256*256) p(01=cont(01)/(256*256) p(10)=count(10)/(256*256) p(11)=count(11)/(256*256) } now display these all as conditional probabilities p0(00)=c0(00)/context(00) similarly all the values then h(00)=p0(00)logbase2(1/p0(00)) h(01) .... ......so on h(11) can we write h(00)= h(AB)?? these 00,01,10,11 are actually AB it is very hard how to write and display all these in matlab? i just wrote in my own words lang. end
assign h(s) entropy= p(AB)H(AB) end
faraz.a
on 28 May 2013
for (d)
step 1 read the image(any bit plane image created in (a) then write it in a 2-d array(matrix)
assign i=1 and j=1 (1=rows, j= columns)
assign A(i,j)=X (this is array)
A(i,j-1)=A
A(i-1,j)=B
now give context=AB (which should be 00,01,10,11 any one of them)
if {
context=00
{
if
X=0
{ c0(00)=1+c0(00) }
else if X=1
{ c1(00)=1+c1(00) (at start c1(00) c0(00) all are equal to zero) }
cont(00)=1+cont(00)
}
else if context=01
{ if x= 0
{
c0(01)=1+c0(01)
}
else if X=1
{
c1(01)=1+c1(01)
}
cont(01)=1+cont(01)
}
else if context=10
{
if X=0
{
c0(10)=1+c0(10)
} else if X=1
{
c1(10)=1+c1(10)
}
cont(10)=1+cont(10)
}
else if context=11
{
if X=0 { c0(11)=1+c0(11)
}
else if X=1
{
c1(11)=1+c(11)
}
cont(11)=1+cont(11)
}
if j<256 (size of the image is 256*256 then we should take 256)
{
j=j+1
}
else if j=256
{ i=i+1
} if i=256 && j=256 (both should 256)
end
else go to
{
assign A(i,j)=X (it should go back to this)
} then i have to find out some float values when displaying this it should c0me steady state probabilities p(00)=cont(00)/(total number of counts i think it will be the size of the matrix 256*256)
p(01)=cont(01)/(256*256)
p(10)=count(10)/(256*256)
p(11)=count(11)/(256*256) }
now display these all the below conditional probabilities p0(00)=c0(00)/cont(00)
p1(00)=c1(00)/cont(00)
p0(01)=c0(01)/cont(01)
p1(01)=c1(01)/cont(01)
p0(10)=c0(10)/cont(10)
p1(10)=c1(10)/cont(10)
p0(11)=c0(11)/cont(11)
p1(11)=c1(11)/cont(11)
similarly all the values then h(00)=p0(00)logbase2(1/p0(00)) +p1(00)logebase2(1/(p1(00)
h(01)=p0(01)logbase2(1/p0(01)) +p1(01)logebase2(1/(p1(01)
so on till h(11)
can we write h(00)= h(AB)?? these 00,01,10,11 are actually AB it is very hard how to write all and display all these
assign h(s) entropy= p(AB)H(AB) end the way i wrote in my notes p(00)h(00)+p(01)h(01)+p(10)h(10)+p(11)h(11) display all these values }
See Also
Categories
Find more on Performance and Memory in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)