how to calculate the probability of each element in string

2 ビュー (過去 30 日間)
juveria fatima
juveria fatima 2018 年 2 月 16 日
コメント済み: Jan 2021 年 6 月 23 日
i am having a rgb image i have converted to string as
rgb_image = imread('lena1.jpg');
% Display the original image
figure(1)
imshow(rgb_image)
title('Original Image');
%convert to string
out=num2str(rgb_image)
now i need to calculate the probability of each pixel value that is now converted to string can anyone help please
  4 件のコメント
juveria fatima
juveria fatima 2018 年 2 月 16 日
yes you r right!
actually i want to find the probability of each pixel value for huffman code
am having a huffman code working for strings so, i thought of converting the image to string amd club that string to my code
here is my code for strings using huffman
%Target: To huffman encode and decode user entered string %-------------------------------------------------------------------------- string=input('enter the string in inverted commas'); %input string
symbol=[]; %initialise variables
count=[];
j=1;
%------------------------------------------loop to separate symbols and how many times they occur
for i=1:length(string)
flag=0;
flag=ismember(symbol,string(i)); %symbols
if sum(flag)==0
symbol(j) = string(i);
k=ismember(string,string(i));
c=sum(k); %no of times it occurs
count(j) = c;
j=j+1;
end
end
ent=0;
total=sum(count); %total no of symbols
prob=[];
%-----------------------------------------for loop to find probability and
%entropy
for i=1:1:size((count)');
prob(i)=count(i)/total;
ent=ent-prob(i)*log2(prob(i));
end
var=0;
%-----------------------------------------function to create dictionary
[dict avglen]=huffmandict(symbol,prob);
% print the dictionary.
temp = dict;
for i = 1:length(temp)
temp{i,2} = num2str(temp{i,2});
var=var+(length(dict{i,2})-avglen)^2; %variance calculation
end
temp
%-----------------------------------------encoder and decoder functions
sig_encoded=huffmanenco(string,dict)
deco=huffmandeco(sig_encoded,dict);
equal = isequal(string,deco)
%-----------------------------------------decoded string and output %variables
str ='';
for i=1:length(deco)
str= strcat(str,deco(i));
end
str
ent
avglen
var
Jan
Jan 2018 年 2 月 16 日
編集済み: Jan 2018 年 2 月 16 日
@juveria fatima: Please format your code properly to make it readable. See http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup .

サインインしてコメントする。

採用された回答

Jan
Jan 2018 年 2 月 16 日
Although Huffman encode works on "strings", you can apply it to numbers also, of course. histcounts will be usual for your purpose.
The code can be simplified. E.g.:
flag=ismember(symbol,string(i)); %symbols
if sum(flag)==0
can be written as:
if ~any(string(i) == symbol)
This is at least confusing:
for i=1:1:size((count)');
Note that size replies a vector and 1:size(x) need not be, what you expect. Transposing the array only to get the wanted dimension as first output of size is a waste of time. Better:
for i = 1:size(count, 2)
or because count is a vector:
for i = 1:numel(count)
But this loop is not required at all:
prob = count / total;
ent = -sum(prob .* log2(prob));
This is very inefficient also:
for i = 1:length(deco)
str = strcat(str, deco(i));
end
The iterative growing of an array is a very bad programming pattern, because the costs grow exponentially. Better without a loop:
str = char(deco);
Finally: num2str(rgb_image) is a very bad idea. Imagine the first pixel has the red channel value 0.2543. Now you convert this by num2str to '0.2543'. Then the Huffman encoding will consider the probability of digits, but you want to encode the probability of colors. This is something completely different.
So start from scratch. 1. improve the code for the Huffman encoding. 2. modify it such, that it accepts numbers from 0 to 255 as inputs. 3. Convert your RGB image to the uint8 format. Afterwards you can apply the Huffman encoding to your image.
  2 件のコメント
Nidhi Kumari
Nidhi Kumari 2018 年 8 月 29 日
Can you give the full code which is actually working properly?
Jan
Jan 2021 年 6 月 23 日
@Nidhi Kumari: The original code was strangly formatted and it would cost some time, to guess, what is exactly meant. If I spend the time to do this, it is not sure, if the code matchs your needs exactly. So better post a new question and try to solve your problem there.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLarge Files and Big Data についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by