You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
move a 3*3 window on a binary image
2 views (last 30 days)
Show older comments
hi I have a 3*3 window and a binary image..I want to move this window on this image... how can do this? thanks
1 Comment
David Young
on 3 Sep 2014
What operation do you want to carry out?
Answers (2)
Image Analyst
on 3 Sep 2014
Try imfilter(), conv2(), or nlfilter(), or possibly blockproc(). That's about all I can say until you provide more information.
17 Comments
Image Analyst
on 3 Sep 2014
So, if in your 3 by 3 window, any of the values in the left most column match any of the values in the right-most column, regardless if they are in the same row or different rows, fill the entire 3by3 window in the output location with the value that matches? What if there are 2 matches, like the (1,1) pixel matches the (2,3) pixels with value 1(true) and the (2,1) pixels matches the (3,3) pixel with value 0? Is the 3x3 output square set to 0 or 1?
Image Analyst
on 3 Sep 2014
What's the overall intent anyway? What do you really want to do, such that you devised this unusual algorithm for? Maybe there's a way to get what you want without doing this unusual thing, which I think will probably be done best with nlfilter().
atefeh gh
on 3 Sep 2014
Image Analyst I want to segment the lung to two region: lungs(zero value) and no lung(one value). when I use a segmentation method there are some bronchiole and nodule parts inside the lung area with intensities close to the lung wall intensities, which are mistakenly converted to the logical value of one, and are detected as non-lung parts .I read in a paper we can solve this problem with the method that I said but base on that you say it cant do this... do you have any suggestion for this? thanks
Image Analyst
on 3 Sep 2014
You can but you have to define the algorithm better. Are you sure they're not just doing a morphological closing ()a local max)? If it's more like what you say then you need to answer by first comment. Maybe you just need to know if there is a 1 in any row of the left column and another 1 in any row of the right column. And, do we need to set the whole 3x3 window in the output image to 1 (very unusual), or just the center pixel where the window is centered (which is nearly every case I've ever seen)? Perhaps you can just do it with two simple calls to imfilter() or conv2() with a clever kernel, followed by thresholding and ANDing.
atefeh gh
on 3 Sep 2014
I attach a file. Maybe it can show my purpose. I think use of 3*3 window is a difficult way Would you agree with me? one thing is not clear for me in this paper: I just know I should use a 3*3 window but I dont know what is the value of this window?
Image Analyst
on 3 Sep 2014
You didn't attach anything, but I don't have much, or any, time for figuring out papers and explaining them to people, though people ask all the time. Sorry but I don't even have time for the papers I'd like to read, much less those that others are reading. Hope you understand. Maybe if it's a short paragraph explaining only that window-scanning part I might be able to figure out what they did, but no promises.
Joseph Cheng
on 3 Sep 2014
Edited: Joseph Cheng
on 3 Sep 2014
It sounds like what is is being attempted is pass a low pass filter (if center value or all windowed pixel are replaced by its surroundings/edges) to get rid of small (based on the 3x3) "holes" or "peaks". If this is going to be a lung or rest of chest cavity binary image then wouldn't imfill() and/or imerode "fill"/"erode" these gaps in the image?
why not try a conv2() of ones(3,3)/9 (ie the average of the 3x3 window) and then putting a threshold based on what majority of ones vs zeroes would make it be replaced.
Image Analyst
on 3 Sep 2014
No it's definitely not a low pass filter, that's a linear filter by the way. What he's asking for is a non-linear filter (median and dilation are some examples of non-linear filters). He wants to ignore the middle column and see if a pixel in the left column matches its partner on the right column (or maybe any pixel in the right column, that's what I'm asking him to tell us). So if you had two lines separated by a space (like edges of a bronchial tube), then it would fill in the space between the edges. It would not affect things outside the tube. Of course this is very dependent on window width and tube width. For example it would not work on edges separated by 3 or 4 columns of space. You'd have to use a bigger window for that and check more pixels. I'm wondering if the window size needs to be adjusted depending on the side of the tubes. Plus it would only work for vertical tubes, not horizontal ones.
I think there may be a way to construct this non-linear filter from two passes of a convolution with special kernels and thresholding but we need a better definition of how the left and right are compared to see how many kernels we'd need and what the threshold values need to be. But it wouldn't be a straight moving average (blurring).
Joseph Cheng
on 3 Sep 2014
Edited: Joseph Cheng
on 4 Sep 2014
Ah i see, it has been a while and mixed the term with what i described with the averaging. Also i was thinking opposite edges included top and bottom of the 3x3 in the consideration of what the middle pixel will be. That is why i was thinking an averaging with a threshold on what constitutes how much of the window needed to be 1's or 0's
Image Analyst
on 4 Sep 2014
You can filter with
1 0 0
1 0 0
1 0 0
with imfilter() to see if there are any "true" pixels on the left in your binary image. And you can filter with
0 0 1
0 0 1
0 0 1
in imfilter() to see if there are any in the right column. Then you can AND those output images together to see if there is at least one pixel in any row in the left column and in any row of the right column. Then you can OR that with your original binary image to fill in (set to 1) any pixel that has non-zero pixels in both the the left and right columns. You can't do it in a single convolution. If you need to match up row 1 with row 1, row2 with row2, and row3 with row3, then you need to just use a 1 row by 3 column kernel instead of a 3x3.
atefeh gh
on 4 Sep 2014
thanks Image Analyst and Joseph I do what you said.the method that Joseph suggest is better for small holes.but it cant covered larger holes...so I think if we first use average and then use a larger filter for larger holes like what Image Analys suggested we can solve this problem ...do you agree with me?? I mean that how can we cover large holes?
Image Analyst
on 4 Sep 2014
No I don't. I don't see why blurring the image would be preferable. Usually when people ask for image processing advice, they attach an image.
Image Analyst
on 5 Sep 2014
OK then if that's what you want, you can blur the image like this
blurredImage = conv2(double(binaryImage), ones(3)/9);
Image Analyst
on 5 Sep 2014
I think that should be the same. Sometimes there are weird things about keeping classes the same so you'd need to check that. For example t2 mgiht be uint8 instead of double and thus not show fractional numbers but just round to 0 and 1. If that happens, just cast to double before sending in to imfilter.
atefeh gh
on 3 Sep 2014
thanks David and Image Analyst I think I can do this by blockproc...I saw in help this: B = blockproc(A,[M N],FUN)
I want to explain the work that I want to do: I should apply 3*3 window based on this: if pexels located on two oposite sides of window have similar logical values(e.g,zero) all pixels covered by this window will be converted to that logical value(e.g,zero)
I want to know Is there any function can halep me to apply my work?? I hope I could explain my work to you.. Thanks in advance
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)