Extract RGB value to decode

I'm trying to create a 2D Colour Barcode steganography which decodes a barcode into its corresponding text. On clicking the "Decode" button, the image must be decoded into its textual form.
I have given red, green, yellow and blue colours for letters A, B, C, D respectively. I want to decode this image by making use of RGB pixel values which will decide what's the letter.
The expected output for above image is ADBCD. Please help me!
[File_Name, Path_Name] = uigetfile('D:\'); %upload image by clicking on "Upload" button
axes(handles.graph)
imshow([Path_Name,File_Name]) %display image on axes in GUI window

回答(1 个)

Walter Roberson
Walter Roberson 2019-4-5

0 个投票

rgb2ind passing in a colormap of your four colors to get color indices.

9 个评论

Would you please explain it with a code?
[File_Name, Path_Name] = uigetfile('D:\');
fullname = fullfile(Path_Name, File_Name);
img = imread(fullname);
letters = {'A', 'B', 'C', 'D'};
cmap = [1 0 0; %red
0 1 0; %green
1 1 0; %yellow
0 0 1]; %blue
lettermap = rgb2ind(img, cmap);
letterimg = letters(lettermap);
function upload_Callback(hObject, eventdata, handles)
[File_Name, Path_Name] = uigetfile('D:\'); %browse to upload image
axes(handles.graph);
fullname = fullfile(Path_Name, File_Name);
read_image = imread(fullname); %read the uploaded image
show_image=imshow(fullname); %display image on the axes
function decode_Callback(hObject, eventdata, handles)
upload; %calling the upload function to get the uploaded image
letters = {'A', 'B', 'C', 'D'};
cmap = [1 0 0; %red
0 1 0; %green
1 1 0; %yellow
0 0 1]; %blue
lettermap = rgb2ind(read_image, cmap);
letterimg = letters(lettermap);
set(handles.output,'string',letterimg); %display the text in the textbox
The output is not obtained. Would you please help me with this?
You create read_image in the workspace of upload_Callback . It is a local variable, and so disappears as soon as upload_Callback returns. It is not available when you call decode_Callback .
I do see one correction to my code:
letterimg = letters(lettermap + 1);
Note that letterimg will be an array with the same number of rows and columns and the original image. Deciding what part to extract from that is a different task. For example I made no effort at all to try to figure out how wide a bar "should" be in order to figure out whether it started ADB or instead starts AADDBB, and no attempt to remove the background area. The code I posted shows how to efficiently take pixels and turn them into numbers by color, and how to convert numbers to corresponding letters.
hints:
  1. you might want to expand cmap to include the background colors.
  2. bwareafilt
I'm new to MATLAB. It would be really helpful if you can write a code. I tried doing this (whatever you have mentioned in your answer), but I couldn't get any results.
This sounds like an assignment. If I were to write the code for you, then you would not be doing the assignment yourself and so would not be able to hand in the code.
I will give a further hint:
mask0 = lettermap == 0; %true only where it was the first color
Now you can remove portions of the mask0 that are too small to be histogram bars, and you can analyze each remaining blob to figure out how wide it is, in order to decide how many letters in a row of that variety there were.
Thanks mate, it's not really an assignment, I'm trying to create this thing as a side-project. I'm figuring out things in MATLAB slowly. Anyway, I'll try this thing too. Thanks for the help!
bwareafilt(mask0, SomeParameterGoesHere) %get rid of small accidental matches
function decode_Callback(hObject, eventdata, handles)
global img %using the global variable 'img' from the "upload" function
index=1; %initialise index of 'text' variable to 1
for i=150:200:1024
red=img(500,i,1); %extract value of 'R' at (500,i)
green=img(500,i,2); %extract value of 'G' at (500,i)
blue=img(500,i,3); %extract value of 'B' at (500,i)
if(red==255&&green==0&&blue==0)
text(index)='A'; %store the letter 'A' in array 'text'
index=index+1; %increment the index for next colour
set(handles.out,'string',text);
elseif(red==0&&green==255&&blue==0)
text(index)='B'; %store the letter 'B' in array 'text'
index=index+1; %increment the index for next colour
set(handles.out,'string',text);
elseif(red==255&&green==255&&blue==0)
text(index)='C'; %store the letter 'C' in array 'text'
index=index+1; %increment the index for next colour
set(handles.out,'string',text);
elseif(red==0&&green==0&&blue==255)
text(index)='D'; %store the letter 'D' in array 'text'
index=index+1; %increment the index for next colour
set(handles.out,'string',text);
end
end
Hey Walter, this is what I tried doing for decoding the image into text. I'm getting output as expected. The only thing I'm lacking in my code is to give automatic spacing between the colours. In my code, I have chosen a spacing of 200 pixels. This works well only if there are 5 columns (colours). If the no. of columns increase, their width will decrease and if the no. of columns decrease, their width will increase. So, the idea of increasing the x-coordinate by 200 will not work everytime. This is the only thing that remains to be done. Would you please help me out?

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by