Applying filter to 2D image using overlap and add method not working

4 views (last 30 days)
Hello,
I am a beginner in MATLAB and am trying to do an assignment related to convolution. Here I am doing the overlap and add method of convolution with a filter h(n) = [1/3, 1/3, 1/3] and input as 32X32 pixel image.
I first converted it into a row vector and then performed the overlap and add method but unfortunately I am getting completely white image even though when I displayed the new images values its not all 255.
CODE:
I= imread("pic1.png");
K = rgb2gray(I);
disp(K);
%imshow(K);
J = K(:);
J = J';
hn = [1/3,1/3,1/3];
M = 3;
N= 2^M;
L = N-M+1;
J = [J zeros(1,2)];
hn_pad = [hn zeros(1,L-1)];
over_add = zeros(1,1026);
xt1 = zeros(1,8);
ccirc1 = zeros(1,8);
ccirc2 = zeros(1,8);
for i = 1:(length(J)/L)
xt1 = J(L*(i-1) + 1:L*(i-1) + L);
xt1 = [xt1 zeros(1,M-1)];
ccirc1 = ifft(fft(xt1).*fft(hn_pad));
for j=1:8
ccirc1(1,j) = uint8(ccirc1(1,j));
end
if i==1
over_add(1:N-M+1) = ccirc1(1:N - M +1);
continue;
else
over_add(L*(i-1)+1:L*(i-1)+2) = ccirc2(N-1:N) + ccirc1(1:2);
over_add(L*(i-1) + 3:L*(i-1) + 6) = ccirc1(3:6);
end
ccirc2 = ccirc1;
end
new_over_add = over_add(1:1024);
final_ans = uint8(zeros(1,1024));
%whos final_ans;
for i=1:1024
final_ans(1,i) = uint8(new_over_add(1,i));
end
%whos final_ans
final_ans = reshape(final_ans,32,[]);
disp(final_ans)
imshow(new_over_add);
The output value of final_ans I obtained was:
85 240 221 193 158 145 201 255 255 255 254 254 255 255 254 255 255 255 255 254 255 255 255 254 255 255 255 201 145 158 193 221
170 255 255 239 218 218 239 255 255 255 255 254 255 255 254 255 255 255 255 255 255 255 255 255 255 255 255 239 218 218 239 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 234 213 213 214 241 255 255 255 255 255 255 255 255 255 255 255 255 255 242 214 214 214 214 234 255 255 255 255
255 255 255 234 170 132 135 137 185 213 243 254 255 255 255 255 255 255 255 255 244 213 186 135 137 137 135 170 234 255 255 255
255 255 234 170 94 60 67 74 113 134 188 243 255 255 255 255 254 255 254 245 191 133 108 64 70 74 68 93 170 234 255 255
85 235 170 93 47 44 53 52 60 63 111 188 244 255 255 255 254 254 255 226 111 51 43 35 43 52 48 46 94 170 235 255
170 199 91 47 56 76 72 58 38 26 42 108 225 255 255 255 255 254 255 218 91 62 52 39 50 63 73 56 47 91 200 255
231 134 37 46 83 94 67 42 62 76 68 93 218 255 255 255 255 254 255 228 144 141 122 65 42 58 86 90 52 37 134 231
195 75 33 68 101 89 45 21 101 148 144 147 229 255 255 255 255 255 255 246 222 222 200 104 27 45 84 110 77 33 75 195
158 33 48 80 95 70 27 8 150 224 180 168 234 255 255 255 255 255 255 240 194 209 244 151 14 33 70 104 88 47 32 158
145 20 58 85 79 51 17 43 190 209 151 122 222 255 255 255 255 255 255 221 121 151 209 190 44 22 54 82 87 57 20 145
145 21 61 76 61 37 10 93 215 151 88 50 204 254 255 255 255 255 255 203 50 88 150 215 92 14 41 61 76 60 21 145
145 20 60 69 53 27 4 102 177 88 65 33 198 251 253 255 255 253 251 198 32 65 87 177 102 7 31 53 68 60 20 145
149 22 52 59 49 21 1 75 119 64 49 32 189 201 235 254 254 236 201 189 33 53 63 119 75 2 22 48 59 52 22 149
182 59 40 51 39 15 14 44 56 48 41 75 151 144 173 245 245 174 144 151 77 46 48 56 44 13 15 35 48 39 59 182
218 103 43 63 27 14 27 46 46 41 74 135 106 84 104 233 233 104 84 105 137 78 41 46 47 24 11 23 59 43 103 218
251 144 59 106 41 18 32 44 43 71 98 144 62 69 51 210 210 51 68 62 145 99 70 43 43 28 15 38 102 59 144 251
255 157 87 151 91 64 46 80 94 93 145 165 65 54 46 148 147 46 53 65 166 145 92 94 79 43 61 91 150 87 157 255
254 191 109 142 132 80 80 149 118 117 170 169 117 64 48 100 94 47 64 117 169 171 116 118 148 79 80 132 143 109 191 255
254 226 128 94 123 78 125 224 131 109 211 192 174 120 52 75 69 51 121 175 192 212 109 131 223 124 77 123 95 128 226 255
254 254 150 71 124 57 134 231 101 111 206 175 205 163 51 96 92 50 164 204 174 206 111 101 230 134 57 124 72 150 254 255
255 255 190 91 105 73 108 157 121 103 147 111 138 141 53 96 93 50 140 138 111 148 103 121 156 108 72 106 91 189 255 255
255 255 228 124 95 101 99 85 127 97 129 95 75 73 50 80 72 42 73 75 95 129 97 127 85 100 101 96 124 227 255 255
255 255 255 131 71 104 76 33 106 111 141 91 47 45 54 56 48 46 44 47 91 141 110 106 32 76 104 72 131 254 255 255
255 255 240 129 83 104 86 36 73 116 157 125 105 106 99 67 62 94 107 106 125 157 116 73 36 87 104 82 128 240 255 255
255 255 202 119 106 105 74 46 51 101 127 124 127 132 147 116 115 148 132 127 124 127 101 51 46 78 109 110 123 202 255 255
255 239 154 104 101 120 86 90 94 85 92 119 129 131 155 136 135 155 132 129 119 92 85 93 89 94 132 114 113 153 239 255
237 191 114 77 93 125 90 147 157 108 101 109 94 92 133 143 143 133 92 93 108 101 108 156 146 97 142 111 87 113 191 237
200 117 96 76 89 125 132 208 228 175 158 139 110 108 149 159 159 149 108 109 139 157 175 227 208 134 138 102 80 95 118 200
185 100 98 71 72 127 192 240 255 227 210 173 160 165 205 214 214 205 165 159 173 211 226 255 240 191 131 76 71 97 100 185
203 147 137 105 90 153 240 255 255 254 253 226 216 220 249 255 255 249 220 216 226 253 255 255 255 240 153 89 105 137 148 203
Could you please help me?
Thank you!

Accepted Answer

Image Analyst
Image Analyst on 13 Jan 2022
Edited: Image Analyst on 13 Jan 2022
Why not simply use conv2()?
If those are the values, the image should not be all white. Try displaying final_and instead of new_over_add:
imshow(final_ans, [], 'InitialMagnification', 2000);
impixelinfo;

More Answers (0)

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!