how to convert an image in to binary bits sequence ??????
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
a=imread('cameramen.jpg');
b= round(a./256);
but i got binary bits like this
1 0 1 0 0 1 0 0 0 0
0 0 0 1 0 1 0 1 0 0
.
.
.
1 0 0 0 0 0 0 0 0 0
0 1 0 1 0 1 0 1 0 0
.
.
1 0 1 0 1 0
0 0 0 0 0 0
but what i want is: b = (1 0 1 0 1 0 0 0 .....n)
採用された回答
Guillaume
2015 年 3 月 4 日
You question is very unclear due to imprecise language.
Are you intending to binarise the image? That is convert each pixel to a single 0 or 1, as you've done with your b = round(a / 256), which convert all pixels below intensity 128 to 0 and all above 127 to 1. Or as your watermarking tag and question title suggest, convert each pixel to a sequence of bits?
If the latter, the best course is actually not to do it and use matlab's bit wise operators. This will be much faster than converting to a matrix of bits and back.
Example, replace LSB of matrix with random 0 or 1:
orig_image = imread('cameraman.tif'); %demo image shipped with matlab
lsb_replacement = uint8(randi([0 1], size(orig_matrix))); %random replacement
%set lsb to 0 (with AND 254) and replace with OR:
new_image = bitor(bitand(orig_matrix, 254), lsb_replacement);
imshowpair(orig_image, new_image, 'montage')
isequal(bitget(new_image, 1), lsb_replacement) %should return true
10 件のコメント
user06
2015 年 3 月 4 日
yes i want to convert each pixel to a sequence of bits. and i m not getting ur code... i need to convert the image into binary sequence of bits and then afterwards i need to access each and every bit.
The point of my code was to show you that in all likelyhood you don't need to convert to individual bits. In my example, I've replaced the least significant bit of each pixel of the image by another value, just using bit operations.
If you really want to convert the image to a sequence of bits, use dec2bin and subtract ascii code of '0' to convert to [0 1]. Depending on the output you want:
option 1: a linear vector (column 1 first, then column 2, etc.)
reshape(dec2bin(orig_image, 8)' - '0', 1, [])
option 2: a 2D matrix with same height as the image and 8 times the number of columns:
reshape(dec2bin(orig_image', 8)' - '0', [], size(orig_image, 1))'
option 3: a cell array of binary representation of each pixel, the cell array is the same size as the image:
reshape(num2cell(dec2bin(orig_image, 8) - '0', 2), size(orig_image))
i have one more doubt.. ok by these statements i got the sequence of bits.. but when i tried to apply run Length code, it is not giving the correct output. and my code for run Length is:
function data=runLenghts(messageInBits)
length=size(messageInBits);
data =[];
str_len=0;
k=1;
for i=1:length
if messageInBits(i)==messageInBits(i+1)
str_len=str_len+1;
else
data(k)=messageInBits(i);
k=k+1;
data(k)=str_len;
k=k+1;
end
end
end
is there some mistake in the code or what???
From a quick read of the code, an obvious mistake is that you never reset the run length str_length to 0 at the end of each run.
Aside from that, although it does not cause problems in your code, you shouldn't be using length as a variable name as it's the name of a matlab function. And numel would be better than size for getting the length of your message.
And of course, vectorising the code would be better:
function rlencoded = runlengthencoding(message)
transitionindices = find(diff(message)) + 1;
transitionlengths = diff([1 transitionindices numel(message)+1]);
valuelengths = [message([1 transitionindices]); transitionlengths];
rlencoded = valuelengths(:)';
end
You could also greatly improve the compression of your RLE by only storing the value of the first bit and then just the run lengths. Since the bit value just flip between 0 and 1 at decoding, you just flip the initial value for each length read.
In my code, the output would then be:
rlencoded = [message(1) transitionlengths]; %and no need to calculate valuelengths
user06
2015 年 3 月 5 日
it is not giving the correct o/p...
suppose i have a message like [1 1 1 0 0 0 0 1 1 1 1 0 0 1 1 0]
so i want o/p as[ 1 3 0 4 1 4 0 2 1 2 0 1]
Hum:
>>runlengthencoding([1 1 1 0 0 0 0 1 1 1 1 0 0 1 1 0])
ans =
1 3 0 4 1 4 0 2 1 2 0 1
Works fine here.
Unless you've done the compression improvement that I mention in my 2nd comment, which of course gives a different result.
If you look at your output the odd elements are alternating 0 and 1, so you only need to know the first one. So [1 3 4 4 2 2 1] is more compact and encode exactly the same information.
user06
2015 年 3 月 7 日
thank u so much..:)
Banhi Das
2023 年 8 月 5 日
please help me with how to reconstruct the image?
The users on this thread have been inactive for three years, so they're unlikely to respond. If you want to ask a question, start a new thread. When you do, attach relevant images and code so that other people know what you have and what you want.
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Watermarking についてさらに検索
参考
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
