im2col error in size

Hi this is my code:
R=rand(162,72);
r=im2col(R,[2 2],'sliding');
R1=col2im(r,[2 2],size®,'sliding');
the size of R is [162 72];
when I use line 1 it is OK. It gives me a a matrix r with size of r=[4 11431]
but at line 2 to obtain R, when I want to convert them to the original matrix via col2im, it give me this error:
"To RESHAPE the number of elements must not change."
Can anybody help me please and tell me what is the problem?

 採用された回答

Image Analyst
Image Analyst 2012 年 7 月 16 日

1 投票

What are you really trying to do? im2col() and col2im() are confusing functions that are probably going to be deprecated soon. How did you ever stumble upon those? Can't you do what you want to do with reshape() or blockproc() or something?

3 件のコメント

Andrea
Andrea 2012 年 7 月 16 日
First of all, Thank you so much for taking your time. I want to use an adaptive non Gaussian filter which needs to have a 3 by 3 neighborhood of image's mean in a sliding way. So I though this function is exactly what I need but I kept receiving error whenever I used col2im! It is something wrong with the defined size I want my results matrix have it (size). So, I take your advise and now trying to figure it out by reshape. Let see what happen.
Image Analyst
Image Analyst 2012 年 7 月 16 日
編集済み: Image Analyst 2012 年 7 月 16 日
I'm glad you explained that because now I can recommend nlfilter() as the proper way to do what you want to do. My advice is to not use reshape() but to use nlfilter(), or alternatively blockproc() if you don't have the Image Processing Toolbox.
Andrea
Andrea 2012 年 7 月 17 日
Thanks so muchhhhh. I will do.

サインインしてコメントする。

その他の回答 (1 件)

Sean de Wolski
Sean de Wolski 2012 年 7 月 16 日

1 投票

Because you are using a 'sliding' block with im2col the number of elements in r is much bigger than the number in R. Compare:
numel(R)
numel(r)
What you have works with 'distinct' blocks as you would expect:
R=rand(162,72);
r=im2col(R,[2 2],'distinct');
R1=col2im(r,[2 2],size(R),'distinct');

5 件のコメント

Andrea
Andrea 2012 年 7 月 16 日
I know that the number of elements in r is much bigger than the number in R. But how can I solve this problem? I do really need "sliding", not "distinct". How can I solve the size issue for "sliding"?
Ryan
Ryan 2012 年 7 月 16 日
Why do you need to re-compress down to the original image size and have it return differently than the original image?
Andrea
Andrea 2012 年 7 月 17 日
Thanks Ryan. Actually in my program, there is no need to comress and recomress the same image. It is just an example. My problem is the error in the size of R, when I want to compress r according to the neighborhood of 2 by 2. And, I did not expect to have different image, Interestingly, I would expect to have the same image, but this code gives me an error.
Image Analyst
Image Analyst 2012 年 7 月 17 日
That won't happen if you don't do that im2col/col2im stuff and use nlfilter instead.
Andrea
Andrea 2012 年 7 月 17 日
編集済み: Andrea 2012 年 7 月 17 日
I know it from your previous answer. Thanks. Both nlfilter and blockproc are amazing. I haven't seen them before! I used them and they perfectly works. Now, I just to realize the difference between these two functions. I should go to the depth a little more. Thank you so much.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeImages についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by