Clear Filters
Clear Filters

Reading images from a folder when a particular condition holds true

1 view (last 30 days)
Hello, suppose i have a folder containing a set of images. In a for loop, every time condition A is true i want a blank image and every time condition B is true i would like one image from the sequence of images in my folder.
for x=1:c
if (c > 440)
c=1;
figure;
else if (c < 440)
Img = imread(fullfile(folderName, Imgs(j).name));
j=j+1;
end
end
end
This is what i have tried. Can you tell me how its done?
  2 Comments
shru s
shru s on 7 Jun 2017
Edited: shru s on 7 Jun 2017
this is the error message -
Index exceeds matrix dimensions.
Error in program (line 62)
Img = imread(fullfile(folderName, Imgs(j).name));
do u know how i can fix this?

Sign in to comment.

Answers (1)

KSSV
KSSV on 7 Jun 2017
Edited: KSSV on 7 Jun 2017
It is because..your j is more then the number of images present...you have to break the loop, if j increases.
N = length(Imgs) ;
for x=1:c
if (c > 440)
c=1;
figure;
else if (c < 440)
Img = imread(fullfile(folderName, Imgs(j).name));
j=j+1;
if j >= N
break
end
end
end
end
Note that, there are better ways to do this.
  3 Comments
shru s
shru s on 7 Jun 2017
close all
img1=imread('C:\Users\Shruthi\Desktop\project\A data\is.jpg');
imshow(img1);
figure;
img1=rgb2gray(img1);
imhist(img1);
threshold=120;
binaryImage = img1 < threshold;
figure;
imshow(binaryImage);
image = bwareaopen(binaryImage,30);
% image= ~image;
figure;
imshow(image);
vp=sum(image,1)
hp=sum(image,2);
plot(vp,'b');
figure;
[le , br ] = size(image);
darkpixels=hp<1;
[lableledregions noofregions]= bwlabel(darkpixels);
fprintf('number of regions =%d\n',noofregions);
z=min(vp);
s=find(vp>z)
e=find(vp==z)
letterLocations = vp > z;
d = diff(letterLocations);
startingColumns = find(d>0);
endingColumns = find(d<0);
folderName = 'C:\Users\p\project\A data\SegmentedCharacters';
Imgs = dir(fullfile(folderName, '*.jpg'));
y=length(vp');
plot(hp,'r');
c=1;
for j=1:y
if vp(j)==z
c = c + 1;
N = length(Imgs) ;
for x=1:c
if (c > 440)
c=1;
figure;
else if (c < 440)
Img = imread(fullfile(folderName, Imgs(j).name));
j=j+1;
if j >= N
break
end
end
end
end
end
end
this is my entire code

Sign in to comment.

Categories

Find more on Image Processing and Computer Vision in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!