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
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
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.
thank u so much..:)
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에서 Image Arithmetic에 대해 자세히 알아보기
참고 항목
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)
