How can I replace value in array by graphic file(pattern)?
Show older comments
I have a picture (matrix containing only values 1,2,3 and 4). I would like to replace each value by different pattern stored in graphic file,for example I would like to replace each value 1 by line pattern, 2 by cross pattern, etc. in order to obtain visual effect of the picture, thanks to different intensity of the pattern.
For example I have pattern like this:
x = [0 0.5 1 1.5 2 2.5 3 3.5 5];
y = [0 2.5 -2.5 2.5 -2.5 2.5 -2.5 2.5 0];
plot(x,y,'k','LineWidth',4);

and I would like to replace every 4 by this pattern.
I would be grateful for any help :)
2 Comments
Image Analyst
on 2 May 2016
What do you mean by "picture" and "matrix"? It looks like you're talking about line plots, not pictures/images.
Accepted Answer
More Answers (2)
1. Load your four patterns into a cell array however you want, e.g:
patterns = cell(1, 4);
for patidx = 1:4
patterns{patidx} = imread(fullfile('C:\somewhere\', sprintf('pattern%d.png', patidx)));
end
2. If need be, resize all the patterns so they're the same size. All patterns need to be the same size for the next step to work.
desiredsize = [10 10];
patterns = cellfun(@(pat) imresize(pat, desiredsize), patterns, 'UniformOutput', false);
3. Use simple indexing and cell2mat to create your final image:
finalimage = cell2mat(patterns(grayimage)); %where grayimage is your image with 4 levels
imshow(finalimage);
2 Comments
buki
on 7 May 2016
Guillaume
on 7 May 2016
When reporting an error, give the entire error message, particularly the part that tells you which line and which instruction is causing the error. Without that information, it's anybody's guess at what the problem is.
A complete guess: the problem is with patterns(grayimage) and that would be because there are more than 4 grey levels in your image.
Image Analyst
on 3 May 2016
1 vote
It looks like that's a raw (not demosaiced) image. Is it?
One way would be to just blur the image a lot.
2 Comments
buki
on 7 May 2016
Image Analyst
on 7 May 2016
If it's a raw image you should demosiac it.
Categories
Find more on Blocked Images 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!


