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 (최근 30일)
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);

답변 (1개)

Omega
Omega 2024년 10월 22일
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:
  1. Variable Names: There's a typo with "DCTTRANSP" and "DCTTRANS". Ensure consistency in naming.
  2. Indexing: There's a problem with how you're indexing the image. You should use "P" instead of "p" in some places.
  3. Inversion: You don't need to manually invert the "DCT" matrix. Use DCT' for the transpose.
  4. Output Image Initialization: Make sure to initialize "iR", "iG", and "iB".
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!

카테고리

Help CenterFile Exchange에서 Convert Image Type에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by