- Variable Names: There's a typo with "DCTTRANSP" and "DCTTRANS". Ensure consistency in naming.
- Indexing: There's a problem with how you're indexing the image. You should use "P" instead of "p" in some places.
- Inversion: You don't need to manually invert the "DCT" matrix. Use DCT' for the transpose.
- Output Image Initialization: Make sure to initialize "iR", "iG", and "iB".
i'm a master student ,i have a home work about DCT, i have the code but it does not work ;please help me
1 view (last 30 days)
Show older comments
i=imread('image065.jpg'); DCT=zeros(8,8); DCT(1:8,1)=1/sqrt(8);
for p=1:8
for q=2:8
DCT(p,q)=1/2 *cos((2*(p-1)+1)*(q-1)*pi/16);
end
end
DCTTRANSP=DCT';
iR8=zeros(8,8);
iG8=zeros(8,8);
iB8=zeros(8,8);
iRDCT=zeros(128,128);
iGDCT=zeros(128,128);
iBDCT=zeros(128,128);
for p=1:16
for Q=1:16
for P=1:8
for q=1:8
iR8(p,q)=i(8*(p-1)+p,8*(Q-1)+q,1);
iG8(p,q)=i(8*(p-1)+p,8*(Q-1)+q,2);
iB8(p,q)=i(8*(p-1)+p,8*(Q-1)+q,3);
end
end
end
end
iR8DCT=DCTTRANS* iR8* DCT;
iG8DCT=DCTTRANS* iG8* DCT;
iB8DCT=DCTTRANS* iB8* DCT;
for p=1:8
for q=1:8
iR8DCT(8*(p-1)+p, 8*(Q-1)+q)=iR8DCT(p,q);
iG8DCT(8*(p-1)+p, 8*(Q-1)+q)=iG8DCT(p,q);
iB8DCT(8*(p-1)+p, 8*(Q-1)+q)=iB8DCT(p,q);
end
end
for p=1:16
for Q=1:16
for P=1:8
for q=1:8
iR8DCT(p,q)= iRDCT(8*(p-1)+p, 8*(Q-1)+q);
iG8DCT(p,q)= iGDCT(8*(p-1)+p, 8*(Q-1)+q);
iB8DCT(p,q)= iBDCT(8*(p-1)+p, 8*(Q-1)+q);
end
end
iR8=inv(DCTTRANSP) * iR8DCT * inv(DCT);
iG8=inv(DCTTRANSP) * iG8DCT * inv(DCT);
iB8=inv(DCTTRANSP) * iB8DCT * inv(DCT);
for P=1:8
for q=1:8
iR(8*(p-1)+p,8*(Q-1)+q)=iR8(p,q);
iG(8*(p-1)+p,8*(Q-1)+q)=iG8(p,q);
iB(8*(p-1)+p,8*(Q-1)+q)=iB8(p,q);
end
end
end
end
ip(:,:,1)=iR;
ip(:,:,2)=iG;
ip(:,:,3)=iB;
ip=uint8(ip);
figure;
imshow(ip);
0 Comments
Answers (1)
Omega
on 22 Oct 2024
Hi Saadi,
I understand that you're encountering issues with the Discrete Cosine Transform (DCT). Here are some issues in your code that need to be addressed:
You can refer to the corrected code below. Ensure the image is 128x128 pixels; if not, adjust the code or resize the image accordingly.
i = imread('sample_image.jpg'); % replace your image
DCT = zeros(8,8);
DCT(1:8,1) = 1/sqrt(8);
for p = 1:8
for q = 2:8
DCT(p,q) = 1/2 * cos((2*(p-1)+1)*(q-1)*pi/16);
end
end
DCTTRANSP = DCT';
iR8 = zeros(8,8);
iG8 = zeros(8,8);
iB8 = zeros(8,8);
iRDCT = zeros(128,128);
iGDCT = zeros(128,128);
iBDCT = zeros(128,128);
for P = 1:16
for Q = 1:16
for p = 1:8
for q = 1:8
iR8(p,q) = i(8*(P-1)+p, 8*(Q-1)+q, 1);
iG8(p,q) = i(8*(P-1)+p, 8*(Q-1)+q, 2);
iB8(p,q) = i(8*(P-1)+p, 8*(Q-1)+q, 3);
end
end
iR8DCT = DCTTRANSP * iR8 * DCT;
iG8DCT = DCTTRANSP * iG8 * DCT;
iB8DCT = DCTTRANSP * iB8 * DCT;
for p = 1:8
for q = 1:8
iRDCT(8*(P-1)+p, 8*(Q-1)+q) = iR8DCT(p,q);
iGDCT(8*(P-1)+p, 8*(Q-1)+q) = iG8DCT(p,q);
iBDCT(8*(P-1)+p, 8*(Q-1)+q) = iB8DCT(p,q);
end
end
end
end
iR = zeros(128, 128);
iG = zeros(128, 128);
iB = zeros(128, 128);
for P = 1:16
for Q = 1:16
for p = 1:8
for q = 1:8
iR8DCT(p,q) = iRDCT(8*(P-1)+p, 8*(Q-1)+q);
iG8DCT(p,q) = iGDCT(8*(P-1)+p, 8*(Q-1)+q);
iB8DCT(p,q) = iBDCT(8*(P-1)+p, 8*(Q-1)+q);
end
end
iR8 = DCT * iR8DCT * DCTTRANSP;
iG8 = DCT * iG8DCT * DCTTRANSP;
iB8 = DCT * iB8DCT * DCTTRANSP;
for p = 1:8
for q = 1:8
iR(8*(P-1)+p, 8*(Q-1)+q) = iR8(p,q);
iG(8*(P-1)+p, 8*(Q-1)+q) = iG8(p,q);
iB(8*(P-1)+p, 8*(Q-1)+q) = iB8(p,q);
end
end
end
end
ip(:,:,1) = iR;
ip(:,:,2) = iG;
ip(:,:,3) = iB;
ip = uint8(ip);
figure;
imshow(ip);
I hope it helps!
0 Comments
See Also
Categories
Find more on Convert Image Type in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!