Matlab: Check all images in a directory for grayscale? - matlab

I want to check all jpg-images in the current directory if they are grayscale or contain coloured pixels... I tried:
figdirectory = pwd;
fullpath = sprintf('%s/*.jpg', figdirectory);
d = dir(fullpath);
% Loop
pages = [];
for i = 1:length(d)
f = d(i).name;
fname_input = sprintf('%s/%s', figdirectory, f);
A = imread(fname_input);
B = rgb2gray(A);
if(A-B == 0)
hascolor = 0;
else
hascolor = 1;
end
pages = [pages; hascolor];
end
but this gives me an error about the matrix dimensions of A and B. Why has A a third dimension?
Thanks!

To do that:
0) Assuming your input is purely an 'image' and not a 'volume'.
1) [r c d] = size(im);
2) if d is larger than 1, then it is a colored image
3) other wise, it has to be a gray-scale image.
EDITS:
You can add one more condition to solidly distinguish grey scale img from color ones. Assuming ur color imgs are of 3 channels,
if d is equal to 3 then check if all 3 channels are equal
im(:,:,1)==im(:,:,2) && im(:,:,1)==im(:,:,3)
Then you have an grayscale image otherwise color img

Here is one simple solution based on IMFINFO:
%# some test images shipped with the Image Processing Toolbox
fNames = {
'circles.png' %# binary
'shadow.tif' %# indexed color
'coins.png' %# gray
'peppers.png' %# RGB
};
isGrayscale = false(size(fNames));
for i=1:numel(fNames)
imgInfo = imfinfo(fNames{i});
if strcmp(imgInfo.ColorType,'truecolor')
isGrayscale(i) = false;
elseif strcmp(imgInfo.ColorType,'grayscale')
isGrayscale(i) = true;
elseif strcmp(imgInfo.ColorType,'indexed')
%# indexed images colormap (the three channels should be all same)
isGrayscale(i) = all(all( diff(imgInfo.Colormap,[],2)==0 ,2),1);
end
end
The first part in your case can be:
dirName = 'C:\path\to\myimages';
files = dir( fullfile(dirName,'*.jpg') );
fNames = {files.name}';
EDIT:
#Adrian: With regards to the image you posed, as far as the image format saved, it IS a color image. Now the fact that all R/G/B channels are all the same is simply a special case...
Anyway if you want to be able to detect such cases, change the 'truecolor' part of the above code to:
#% ...
if strcmp(imgInfo.ColorType,'truecolor')
img = imread(fNames{i});
isGrayscale(i) = isequal(img(:,:,1),img(:,:,2),img(:,:,3));
elseif strcmp(imgInfo.ColorType,'grayscale')
#% ...

You can use IMFINFO for this task, so that you won't have to load the image into memory.
figdirectory = pwd;
fullpath = sprintf('%s/*.jpg', figdirectory);
d = dir(fullpath);
nImages = length(d);
%# imageType is a cell array with either 'grayscale', 'truecolor', or 'indexed',
%# depending on the kind of image you have.
imageType = cell(nImages,1);
for iImg = 1:nImages
info = imfinfo(d(iImg).name);
imageType{iImg} = info.ColorType;
end
%# isGrayscale is true for grayscale images, false otherwise
%# (though note that there might be mapped images that map to a grayscale colormap).
isGrayscale = cellfun(#(x)strcmp(x,'grayscale'),imageType);
EDIT
%# the indexed images have to be loaded in order for you to check
%# for grayscale maps
indexedIdx = find(cellfun(#(x)strcmp(x,'indexed'),imageType));
for iImg = indexedIdx(:)'
[~,map] = imread(fullfile(figDirectory,d(iImg).name));
%# It's a grayscale image if rgb map values are all equal
isGrayscale(iImg) = all(all(bsxfun(#eq,map,map(:,1)),2),1);
end
%# finally, it is possible that the images are *stored* as truecolor
%# but containing, in fact, a grayscale image
truecolorIdx = find(cellfun(#(x)strcmp(x,'truecolor'),imageType));
for iImg = truecolorIdx(:)'
img = imread(fullfile(figDirectory,d(iImg).name));
%# It's a grayscale image if rgb map values are all equal
isGrayscale(iImg) = all(all(all(bsxfun(#eq,img(:,:,1),img),1),2),3);
end

From the manual for imread http://www.mathworks.co.uk/help/techdoc/ref/imread.html
If the file contains a grayscale image, A is an M-by-N array. If the
file contains a truecolor image, A is an M-by-N-by-3 array.
So A will have a third dimension for the coloured images.
If you want to test for greyscale then you could convert the image to hsv.
B = rgb2hsv(A)
from the manual, http://www.mathworks.co.uk/help/techdoc/ref/hsv2rgb.html
When B(:,2) is 0, the colors are unsaturated (i.e., shades of gray).
In the case of a true colour image like the one posted it will be if unique(B(:,:,2)) is zero, hence
A = imread(fname_input);
B = rgb2hsv(A);
%# if saturation levels are zero then it's a greyscale image
if(unique(B(:,:,2)) == 0)
hascolor = 0;
else
hascolor = 1;
end

Related

How to apply median filter in all images and store in a directory?

f = 'C:\Users\HP\Desktop\images';
d = ls(f);
d(1,:)=[]
d(1,:)=[]
mkdir('New_images')
for i=1:size(d)
I=imread(fullfile(f,d(i,:)));
Kmedian = I;
for i = 1:3
Kmedian(:,:,i) = medfilt2(Kmedian(:,:,i));
end
Kmedian=imresize(Kmedian,[227 227]);
imshow(Kmedian)
imwrite(Kmedian,fullfile('New_images',strcat(num2str(i),'.jpeg')));
end
How to use median filter at once.
Errors:
In resize (line 5)
Index in position 3 exceeds array bounds (must not exceed 1).
Error in resize (line 10)
Kmedian(:,:,i) = medfilt2(Kmedian(:,:,i));
The medfilt2 function only works on 1 channel at the time. If you have a color image, you will have a 3rd dimension to your pixel "matrix" for red, green, blue channels respectively.
You can still use the medfilt2 function by either converting your image to grayscale, or apply it to each channel individually.
Kmedian = I;
for i = 1:size(Kmedian,3) % Iterate over all the channels in the image
Kmedian(:,:,i) = medfilt2(Kmedian(:,:,i));
end
Updated to account for both single and multi channel images

Spheroids detction from Images

I need to find the blobs from the below image.
The major problem is background. background doesn't have uniform intensity. I tried couple of things like thresholding and edge detection in MATLAB but couldn't able to find a better ways to segment out all spheroids. I need to extract the blobs and I need to find the area of each blobs. Does anyone know how to work-around with this kind of background?
Edit (07/02/17):
As suggested by Spektre I tried Following things in MATLAB.
Method 1:
img_original = imread('~/my_image.jpg'); %Read image
img_ch = single(img_original(:,:,2)); %Pick one channel( here its green)
g = fspecial('gaussian',200,100); %Kernel matrix to make the img blurr
con_img = conv2(img_ch,g,'same'); %2D convolution, this wil make the img blurr
sub_img = (con_img - img_ch); %Simple matrix subtraction
sub_img(sub_img <= 10) = 0; %Thresholding
sub_img(sub_img ~= 0) = 1;
fil_sub = imfill(sub_img,'holes'); %Fill the regions
imgfilt = imfilter(fil_sub, ones(3)); %run filter using 3by3 matrx
imgfilt(imgfilt < 8) = 0; %Reduce noisy pixels by thresholding
mfilt_img = (medfilt2(imgfilt)); %reduce unwanted pixels
img = img_ch;
img(mfilt_img ~= 0) = 255;
img2 = img_ch;
img2(img2 < 70) = 0; %Threshold for darker pixels which are left out from above methode.
img(img2 ==0) = 255;
disp_img = img_original(:,:,1);
disp_img(img ==255) = 255;
img_original(:,:,1) = disp_img;
figure, imshow(img_original)
I got the segments but still not good enough I think. This method gave good segments in the high intensity background, Even if I reduce the threshold value segments are not clear in the darker background and brightest pixels in the blobs are excluded.
Method 2:
img_original = imread('~/cancer_cells/Snap-10234.jpg'); %Read image
img_ch = single(img_original(:,:,2)); %Pick one channel( here its green)
clear new_matcel cur_img matcel avg_matrx
s=3; % Set size of the square window
mat_img = img_ch; % Working image channel
% resize the working matrix so that the dimensions matches
resize_img = resizem(mat_img,round(size(mat_img)/s)*s);
% convert matrix into small s x s matrix and save each in cells
window_c = ones(1,size(resize_img,1)/s) * s;
window_r = ones(1,size(resize_img,2)/s) * s;
mat_cel = mat2cell(resize_img,window_c,window_r);
new_matcel = cell(size(mat_cel)); % initialize new variable
% get the average value for each window and replace the actual by avg value
for i = 1:size(mat_cel,1)
for j = 1:size(mat_cel,2)
cur_img = mat_cel{i,j};
avg_value = mean(mean(cur_img));
new_matcel{i,j} = ones(s) * avg_value;
end
end
avg_matrx = cell2mat(new_matcel); % convert cells to matrix
image_sub = (abs(resize_img - avg_matrx)); % take the absolute difference
image_sub(image_sub < 7) = 0; % thresholding
image_sub(image_sub~=0) = 1;
image_sub = bwmorph(image_sub,'bridge');% fill gaps
image_sub = imfill(image_sub,'holes'); % fill the bounded regioons
% image_sub(image_sub == 1) = 255;
image_sub = resizem(image_sub,size(img_ch)); % resize to original size
disp_img = img_original(:,:,1);
disp_img(image_sub == 1) = 255;
img_original(:,:,1) = disp_img;
figure, imshow(img_original)
Much better segmented image:
even brighter pixels are included in the segment. Thanks to Spektre.
Is there a way to improve the above code? or any other idea to get more precise segments?
Thanks.

Error on Extract hog Features from image sets of different dimensions in Matlab. ''Subscripted assignment dimension mismatch''

How could I perform hog feature extraction from image set of different dimensions? I am getting this error on last section.
''Subscripted assignment dimension mismatch.''
%% Change Directory
cd('C:\Users\inquisitive\Documents\MATLAB\hog_test');
%% Root Folder
rootFolder = fullfile('D:\HGDatabase');
%% Load Imagesets Database Directory
imgSets = imageSet(rootFolder,'recursive');
%%
{ imgSets.Description } % display all labels on one line
[imgSets.Count] % show the corresponding count of images
%% determine the smallest amount of images in a category
minSetCount = min([imgSets.Count]);
imgSets = partition(imgSets, minSetCount, 'randomize');
[imgSets.Count]
%% partioning into training and testing sets
[trainingSet, testSet] = partition(imgSets, 0.2, 'randomize');
*%%CAN'T I SKIP THIS SECTION AND DEFINE hog_16x16 ON LOOP ITSELF WHY I NEED TO PERFORM THIS AT FIRST WITH ONE IMAGE*
img = read(trainingSet(3), 4);
[hog_16x16, vis16x16] = extractHOGFeatures(img,'CellSize',[16 16]);
cellSize = [16 16];
hogFeatureSize = length(hog_16x16);
%%Extracting Hog Features
trainingFeatures = [];
trainingLabels = [];
for hand = 1:numel(trainingSet)
numImages = trainingSet(hand).Count;
features = zeros(numImages, hogFeatureSize, 'single');
for i = 1:numImages
img = read(trainingSet(hand), i);
% Apply pre-processing steps
lvl = graythresh(img);
img = im2bw(img, lvl);
features(i, :) = extractHOGFeatures(img, 'CellSize', cellSize);
end
labels = repmat(trainingSet(hand).Description, numImages, 1);
trainingFeatures = [trainingFeatures; features];
trainingLabels = [trainingLabels; labels ];
end
Other Query:
Which image format work best .jpg or .png? I thought .png would be better? Am i right?
I took some pictures and resize them by thinking that it may train in better way? Am i thinking right or wrong? Or is there nothing to do with different size of input. What would you suggest either to perform in same size or image sets with different dimensions.
CAN'T I SKIP FOLLOWING SECTION AND DEFINE hog_16x16 ON LOOP ITSELF WHY I NEED TO PERFORM THIS AT FIRST WITH ONE IMAGE? If i can skip how to deal with that case
img = read(trainingSet(3), 4);
[hog_16x16, vis16x16] = extractHOGFeatures(img,'CellSize',[16 16]);
cellSize = [16 16];
hogFeatureSize = length(hog_16x16);
You should ensure the dimensions of the image are the same as all images, then your extracted hog_16x16 features will have the same dimensions.

how do i mask labeled object based on some specified threshold value for each objects area,majoraxis and minoraxis?

i am currently using bwconnomp to label each connected object and regionpropsto find area, majoraxis, minoraxis of each labeled object respectively. i am also displaying each labeled object its area,majoraxis and minoraxis. now i want to set some threshold for area,majoraxis and minoraxis and if the value of area,majoraxis and minoraxis is above specified threshold then that object has to be masked.how this can be done??
here is my code
clc
clear all
close all
Index = 1;
scrsz = get(0,'ScreenSize');
%read an image
while Index ~= 0
% Open a dialog and select an image file
[FileName,FilePath,Index] = uigetfile('*.png', 'Open Imagefile ');
if Index == 0
disp('Procedure Done')
break;
end
inimage = imread([num2str(FilePath) FileName]);
D=inimage;
A=inimage;
subplot(2,3,1);
imshow(inimage);
title('original image');
%labeling algorithm
B=im2bw(inimage);
C=imfill(B,'holes');
label=bwlabel(C);
max(max(label))
CC = bwconncomp(B);
data = regionprops(CC,'all');
for j=1:max(max(label))
[row, col] = find(label==j);
len=max(row)-min(row)+2;
breadth=max(col)-min(col)+2;
target=uint8(zeros([len breadth] ));
sy=min(col)-1;
sx=min(row)-1;
for i=1:size(row,1)
x=row(i,1)-sx;
y=col(i,1)-sy;
target(x,y)=A(row(i,1),col(i,1));
end
mytitle=strcat('Object Number:' ,num2str(j),'area:', num2str(data(j).Area),'MajorAxis: ',num2str(data(j).MajorAxisLength),'MinorAxis: ',num2str(data(j).MinorAxisLength));
figure,imshow(target);title(mytitle);
a=size(target);
ax=a(1);
ay=a(2);
pos=[1,1,ay,ax];
rectangle('Position',pos,'EdgeColo','r')
end
next = input('next image? press Enter: ');
if next == 0
channelactivity = 0;
break
else
close all
disp('==================================')
pause(0.2)
continue
end
end
Here is a way to do it. The code is commented so easy to follow; the important line is the following:
AboveAreaIndices = find(vertcat(data.Area) > SomeValue)
In which you store the indices of the objects whose area is larger than SomeValue. In the example I color them red but you can do whatever you want with them or remove them altogether from the data structure.
You can also use logical operators to combine multiple conditions for example using the MinorAxis and MajorAxis properties. Note that I used AllArea as anew variable to store the concatenated areas to make things clearer, but you can keep them as vertcat(data.Area).
AboveIndices = find(vertcat(data.Area) > SomeValue & vertcat(data. MinorAxis) > SomeValue & Bla bla bla...);
Whole code:
clear
clc
close all
%// Read and clean up sample image
A = imread('rice.png');
A = im2bw(A,.5);
A = bwareaopen(A,50);
CC = bwconncomp(A);
%// Same as you.
data = regionprops(CC,'all');
%// Concatenate all the areas into an array.
AllArea = vertcat(data.Area);
%//========================================
%//==== Apply threshold on area here \\====
AboveAreaIndices = find(AllArea > 150);
%// If you wish to remove the entries from the data structure
% data(AllArea>150) = [];
%//========================================
%// Same for centroids...for display purposes
AllCentroids = vertcat(data.Centroid);
%// Display original and thresholded objects. Use the indices calculated
%// above to "mask" large areas if you want
imshow(A);
hold on
scatter(AllCentroids(:,1),AllCentroids(:,2),40,'b','filled')
scatter(AllCentroids(AboveAreaIndices,1),AllCentroids(AboveAreaIndices,2),40,'r','filled')
And sample output:

How to convert rgb to gray but keep one colour unchanged

I have an image in which I want to only keep the green part and convert the others to gray scale.
It seems from the colors in your image, that the greens are actually so close to grayscale that you'll be unable to see anything. Either way this is how you do it:
im = imread('houses.jpg');
s= (im(:,:,2)>im(:,:,1))&(im(:,:,2)>im(:,:,3));
dim = double(im);
r = abs(dim(:,:,2)-((dim(:,:,1))+dim(:,:,2)+dim(:,:,3))/3) < 3;
q = s|r;
imgs = rgb2gray(im);
imgs3(:,:,1) = imgs;
imgs3(:,:,2) = imgs;
imgs3(:,:,3) = imgs;
imout = zeros(size(im));
for i=1:size(im,1),
for j=1:size(im,2),
if q(i,j) == 0,
imout(i,j,:) = imgs3(i,j,:);
else,
imout(i,j,:) = im(i,j,:);
end
end
end
imshow(uint8(imout))
imwrite(uint8(imout),'output.jpg');
if you want to see something useful you would probably want to do the following
im = imread('houses.jpg');
s= (im(:,:,2)>im(:,:,1))&(im(:,:,2)>im(:,:,3));
dim = double(im);
r = abs(dim(:,:,2)-((dim(:,:,1))+dim(:,:,2)+dim(:,:,3))/3) < 3;
q = s|r;
imgs = rgb2gray(im);
imgs3(:,:,1) = imgs;
imgs3(:,:,2) = imgs;
imgs3(:,:,3) = imgs;
imout = zeros(size(im));
for i=1:size(im,1),
for j=1:size(im,2),
if q(i,j) == 0,
imout(i,j,:) = imgs3(i,j,:);
else,
imout(i,j,1) = im(i,j,1);
imout(i,j,2) = 255;
imout(i,j,3) = im(i,j,3);
end
end
end
imshow(uint8(imout))
imwrite(uint8(imout),'output.jpg');
which gives this as result:
It should be something like this:
gray_img = rgb2gray(img);
green_mask = img(:,:,2) > img(:,:,1) & img(:,:,2) > img(:,:,3);
gray_img3 = cat(3, gray_img, gray_img, gray_img);
green_mask3 = cat(3, green_mask, green_mask, green_mask);
output_img = gray_img3;
output_img(green_mask3) = img(green_mask3);
I didn't check it, but I'm quite sure it should do the work.
The only thing you might want to modify is the computation of the mask, currently it will include all pixels that are more green than red or blue, but you may want to be more (or less) specific in choosing which pixels to leave unchanged.
I would suggest to use a purely hue=based approach, as you are interested in a specific color.
My approach would be to convert your images to the HSV colorspace and check the Hue channel.
img=imread('houses.jpg');
hsv=rgb2hsv(img);
% find the white areas of the image
gray_part=hsv(:,:,3)>0.7;
% find the green areas of the image that are not white
green_part=hsv(:,:,1)>(30/360) & hsv(:,:,1)<(210/360) & ~gray_part;
% create the output image
outimg=(uint8(double(img).*repmat(green_part,[1 1 3])));
figure;
subplot(121); imshow(img);
subplot(122); imshow(outimg);
This will give you the following image (left: original, right: resulting masked image):
Of course, if you need to get the rest of the image in grayscale you may use the previous results as follows:
pixel(1,1,1)=0;pixel(1,1,2)=255;pixel(1,1,3)=0;
green_img=repmat(pixel,[size(img,1) size(img,2), 1]);
green_part=repmat(green_part,[1 1 3]);
gray_img=rgb2gray(uint8(double(img).*~green_part));
gray_img=double(repmat(gray_img,[1 1 3]));
outimg=uint8(green_part.*green_img + gray_img);
figure; imshow(outimg);
This will display the image shown below: