Loop Problem

2 Ansichten (letzte 30 Tage)
Nil
Nil am 15 Mär. 2012
Hi,
Overview on Question
I am working on image steganography code and s, n, p are calculated values which I am getting as per requirement but struggling to get “Bits_to_Modify ” column result shown in below table
Output of Existing program
==========================================================
My text=’ab’
TextLength=2
Binary representation:
s= 0110000101100010
n=8 (Textlength*4)
p=
2
0
1
2
0
1
2
0
=============================================================
First converting text to bytes, then calculating index variable (called as p) & formula is MOD(No of Bytes,3).
I have text length as 2 bytes so Index Variable (P) is 2..
I will always have index variable values as 0 or 1 or 2 which will be based on no. of Bytes I have assigned
for first two bits (01) , index variable assigned as 2
for next two bits (10), index variable assigned as 0
for next two bits (00), index variable assigned as 1
for next two bits (01), index variable assigned as 2..
so on till end of my bits.
I want to modify bits of pixels (4th pixel onwards) as per my value of index variable.
In 5th Pixel need to embed the data bits 01 in 6th and 8th bit locations, and next value of p becomes 0. Need to embed the next data bits 10 into 6th Pixel in 6th and 7th bit locations, next value of p becomes 1. Now need to embed the next two bits 00 into 7th Pixel in 7th and 8th bit locations and so on.
Pixcel IndexVariable Bits_to_Modify
5 2 b6=0 b8=1
6 0 b6=1 b7=0
7 1 b7=0 b8=0
8 2 b6=0 b8=1
9 0 b6=0 b7=1
10 1 b7=1 b8=0
11 2 b6=0 b8=0
12 0 b6=1 b7=0
Question
I have written the following program taking above values of S,n & P which i am getting
but it dont give following output..Please provide me direction
for j=0:2:2*n-2
for i=1:1:n
if p(i)==2
p1= p(i)
pv=(i+4)
b6=s(j+1)
b8=s(j+2)
end
if p(i)==0
p1= p(i)
pv=(i+4)
b6=s(j+1)
b7=s(j+2)
end
if p(i)==1
p1= p(i)
pv=(i+4)
b7=s(j+1)
b8=s(j+2)
end
end
end
Expected Output
p1=2
pv=5
b6=0
b8=1
p1=0
pv=6
b6=1
b7=0
p1=1
pv=7
b7=0
b8=0
p1=2
pv=8
b6=0
b8=1
p1=0
pv=9
b6=0
b7=1
p1=1
pv=10
b7=1
b8=0
p1=2
pv=11
b6=0
b8=0
p1=0
pv=12
b6=1
b7=0
  1 Kommentar
Nil
Nil am 15 Mär. 2012
Can anyone pls point wrong logic in my logic

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Kevin Holst
Kevin Holst am 15 Mär. 2012
You have two loops when you just need one. Essentially you're looping through the entire binary variable for each p value, but each p value corresponds to 2 bits in your binary variable (my understanding). Try this, it gets the right answer:
for i=1:1:n
if p(i)==2
p1= p(i)
pv=(i+4)
b6=s(2*i-1)
b8=s(2*i)
end
if p(i)==0
p1= p(i)
pv=(i+4)
b6=s(2*i-1)
b7=s(2*i)
end
if p(i)==1
p1= p(i)
pv=(i+4)
b7=s(2*i-1)
b8=s(2*i)
end
end
  1 Kommentar
Nil
Nil am 16 Mär. 2012
Thanks Kevin for your help.you are right..i too reliazed the same and just kept one loop but two variable i & j but initialized i before loop..your suggestion is more correct.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by