How to convert RGB matrix to binary array in MATLAB - matlab

I wrote a code to convert image(jpg) to binary using Matlab for bidirectional visible light communication. there are some mistakes. I need to create RGB matrices and save binary values in an array using a nested loop. after that, I need to send binary values for the LED driver. I did some coding parts. can you help me to do this?
i=imread('imageName.jpg');
R = i(:,:,1); %red
G = i(:,:,2); %green
B = i(:,:,3); %blue
for i=1:183
for j=1:275
R(i,j);
binStr = dec2bin(R(i,j));
end
end

Related

How can I modify a set of vectors to have the same size?

i'm trying to extract HOG_features for mathematical symbols classification (i will use SVM classifier). I get a 1xn vector then i have to put all the vectors in a single matrix. The problem is that the size of the feature vector is different for each image so I can't concatenate them.
Is there a way to make all vectors having the same size ?
Thank you in advance.
Here is the code:
rep1 = 'D:\mémoire MASTER\data';
ext = '*.tif' ;
chemin = fullfile(rep1, ext);
list = dir(chemin);
for i=1:length(list)
I = imread(fullfile(rep1, list(i).name), ext(3:end));
if size(I,3)==3 % RGB image
I = rgb2gray(I);
end
I1 = imbinarize(I);
% Extract HOG features data
HOG_feat = extractHOGFeatures(I1,'CellSize', [2 2]);
HOG_feat1 = HOG_feat';
end
You can pad each one with zeros to be as long as the longest one:
e.g. to put two vectors, v1 and v2, into a matrix M:
M = zeros(2,max(length(v1),length(v2)));
M(1,1:length(v1)) = v1;
M(2,1:length(v2)) = v2;
You have the problem that all your vectors are a different size. Instead of trying to coerce them to be the sesame size by zero-padding or interpolating (both I think are bad ideas), change your computation so that the length of the output vector does not depend on the size of the image.
This is your current code:
HOG_feat = extractHOGFeatures(I1,'CellSize', [2 2]);
% ^^^
% the image is split in cells of 2x2 pixels
2x2 cells are way too small for this method anyway. You could instead divide your image into a set number of cells, say 100 cells:
cellSize = ceil(size(I1)/10);
HOG_feat = extractHOGFeatures(I1,'CellSize', cellSize);
(I’m using ceil in the division because I figure it’s necessary to have an integer size. But I’m not sure whether ceil or floor or round is needed here, and I don’t have access to this function to test it. A bit of trial and error should show which method gives consistent output size.)

How can i change some string values to numeric values on data imported from Excel in matlab

I'm dealing with NSL-KDD data set and i want to change some strings to numeric values using matlab and here's my code
a = xlsread ('20 Percent Training Set.xls'); % normal data
[n,p]= size (a);
% calculate mean for normal data
for z =1:n
for v=1:p
b = a(z,v);
if strcmp(b,'tcp')
b=1;
end
end
end
a
but when i print the matrix a the NaN values still unchanged, is there something i can do, thanks.
The problem is that you are changing b, not a. You need to set a(z,v)=1. Therefore, your code should be:
a = xlsread ('20 Percent Training Set.xls'); % normal data
[n,p]= size (a);
% calculate mean for normal data
for z =1:n
for v=1:p
b = a(z,v);
if strcmp(b,'tcp')
a(z,v)=1;
end
end
end
a

detect lines in image matlab

Hello I have a work on this image:
My objective is to count all the sperm in this image .for this I'm thinking to detect just the lines so it make my work easy. because I am beginner, in this step I am completely lost there are any algorithms can help me to detect lines?? ( I have seen that there are hough transformation and scan line algorithm) I don't which algorithm can help me and if there are others
Here's a piece of code that might help you getting started.
By looking at the image it seems very difficult to label sperms by looking at straight lines and hence using Hough transform won't help a lot.
In the example below I focused on filtering the image and counting the number of blobs. The code is commented and should be easy to understand.
img = imread('d9S3Z.png');
figure, imshow(img)
% convert to binary image
[X,map] = rgb2ind(img,0.0);
img = ind2gray(X,map); % Convert indexed to grayscale
level = graythresh(img); % Compute an appropriate threshold
% or use your own, e.g. level = 0.46
img_bw = im2bw(img,level);% Convert grayscale to binary
% create mask to remove edge interference
mask = zeros(size(img_bw));
mask(2:end-2,2:end-2) = 1;
img_bw(mask<1) = 1;
%invert image
img_inv =1-img_bw;
% find blobs
img_blobs = bwmorph(img_inv,'majority',10);
figure, imshow(img_blobs);
% count blobs
CC = bwconncomp(img_blobs);
num_sperm = CC.NumObjects # sperm count

How to save intensity value without loosing precision of image data in matlab

Lets say we have a simple RGB image on disk.
If we read the image using imread() it gets stored as uint8 type. Then I do Discrete Fourier Transform on it using fft(), the returned image is of double type.
Now, on storing this data as image on disk, using imwrite(), and again read using imread(), the precision of data seems to get lost and again the image is stored as uint8.
This means, I can't operate Inverse Discrete Fourier Transform on saved DFT image! How should I approach this one?
You need to use an file format that supports floating point images. Tiff is one of them. imwrite doesn't have the flexibility so you need to use the Tiff class. This will not work with complex data. If it's complex I would use matlabs mat format.
function imwriteDouble(filename,data)
% imwriteDouble(filename,data) saves floating point data in tiff image
n=ndims(data);
t=Tiff(filename,'w');
tagstruct.ImageLength = size(data,1);
tagstruct.ImageWidth = size(data,2);
tagstruct.BitsPerSample = 64;
if n==2
tagstruct.SamplesPerPixel = 1;
tagstruct.Photometric = Tiff.Photometric.MinIsBlack;
elseif n == 3
tagstruct.SamplesPerPixel = 3;
tagstruct.Photometric = Tiff.Photometric.RGB;
else
error('Image must have 2 or 3 dimensions');
end
tagstruct.Compression=Tiff.Compression.None;
tagstruct.SampleFormat=Tiff.SampleFormat.IEEEFP;
tagstruct.PlanarConfiguration = Tiff.PlanarConfiguration.Chunky;
t.setTag(tagstruct);
t.write(data);
t.close();
end
You can read the image back using imread.

How can I invert a binary image in MATLAB?

I have a binary image and need to convert all of the black pixels to white pixels and vice versa. Then I need to save the new image to a file. Is there a way to do this without simply looping over every pixel and flipping its value?
If you have a binary image binImage with just zeroes and ones, there are a number of simple ways to invert it:
binImage = ~binImage;
binImage = 1-binImage;
binImage = (binImage == 0);
Then just save the inverted image using the function IMWRITE.
You can use imcomplement matlab function. Say you have a binary image b then,
bc = imcomplement(b); % gives you the inverted version of b
b = imcomplement(bc); % returns it to the original b
imwrite(bc,'c:\...'); % to save the file in disk
In Matlab, by using not we can convert 1's into 0's and 0's into 1's.
inverted_binary_image = not(binary_image)
[filename, pathname] = uigetfile({'*.bmp'},'Text as image');
img=imread(filename);
img=im2double(img);
[r,c,ch]=size(img);
%imshow(img);
invert_img=img;
if(ch==1)
for i=1:r
for j=1:c
if(invert_img(i,j)==0)
invert_img(i,j)=1;
else
invert_img(i,j)=0;
end
end
end
end