-2

I was given a defined set of images (.png), I am supposed to detect each images Edges, then apply some image processing, but I have a problem. First I an image array as follows :

imgArray = {'image_1.png','image_2.png','image_3.png'}

Then applied edging (sobel), using the MATLAB built in function edge so :

for i = 1:3
         image=imread(imgArray{i});
         image = edge(image,'sobel');
         imgArray{i} = image;
end

based on that prvious code and my understanding, that the imageArray, now contains all 3 edged images. Later on, I need to use the Edged images using that command image=imread(imgArray{i}); in a different place in the code, but it gives me an Error, I dont understand why does that happen ??

EDIT:

Here's the error I'm getting:

Error in ==> ImageCompare at 43 image=imread(imgArray{i});
TylerH
  • 20,799
  • 66
  • 75
  • 101
user1111726
  • 157
  • 2
  • 8
  • 18
  • Thats the Exact Error : Error in ==> ImageCompare at 43 image=imread(imgArray{i}); – user1111726 Mar 29 '12 at 21:32
  • the last line in the loop redefines each element `imgArray`. Try using a new variable name to store the resulting edged image eg `edgedImage{i}=image;` instead. – Azim J Mar 29 '12 at 21:34
  • I thought of that, but the problem is that previous code is used in an "if condition" , so if the "if condition" is not used , I still need to process the original images (without Edging) , therefore I cant use a new variable to store the results in it. – user1111726 Mar 29 '12 at 21:36

2 Answers2

2
imgArray = {'image_1.png','image_2.png','image_3.png'};
imgArrayEdged = strrep(imgArray, '.png', '_edged.png');
for i = 1 : length(imgArray)
    image = imread(imgArray{i});
    image = edge(image,'sobel');
    imwrite(image, imgArrayEdged{i});
end

% later...

for i = 1 : length(imgArray)
    if (your_condition)
        image = imread(imgArray{i});
    else
        image = imread(imgArrayEdged{i});
    end
end
Serg
  • 13,470
  • 8
  • 36
  • 47
  • Yes thank you, I think you solved the problem , but just one question, I dont really understand the second line of the code ?? – user1111726 Mar 29 '12 at 22:15
  • it's a fast way to create new filenames imgArrayEdged ={'image_1_edged.png' 'image_2_edged.png' 'image_3_edged.png'} by replacing every ".png" into "_edged.png" – Serg Mar 29 '12 at 22:29
  • Just one more question, the new written files )'image_1_edged.png' 'image_2_edged.png' 'image_3_edged.png') can I in some way delete them at the end, cause I dont need them anymore ? – user1111726 Mar 29 '12 at 23:48
  • why don't you google "matlab delete files"? There is a function delete(filename) in Matlab – Serg Mar 30 '12 at 10:37
0

Your imgArray contains file names as strings. In your loop, you are reading the image files and replacing each string in the cell array with image data.

If you absolutely require the file name strings later, you must create a second variable to hold the image data itself. If you only require the original images, just don't use imread later in the code!

Having read in an image file with imread once, there is no reason to waste time by reading the files again. It seems like you aren't quite aware of what state your data is in as it moves through your code. I suggest you use MATLAB's excellent debugger to step through and examine the type and content of the variables - you will quickly see where imread, which needs a file name, is inappropriate.

reve_etrange
  • 2,561
  • 1
  • 22
  • 36