史蒂夫(Steve)与MATLAB进行图像处理

图像处理概念,算法和MATLAB

不要photoshop ... matlab它!MATLAB的图像效应(第4部分)

I'd like to welcome back guest blogger布雷特·肖尔森(Brett Shoelson)为了the continuation of his series of posts on implementing image special effects in MATLAB. Brett, a contributor for the文件交换Pick of the Week blog,已经与MATLAB进行图像处理已有近20年了。

[第1部分][第2部分][第3部分][Part 4]

内容

Out Standing in the Field

In the three previous entries in this guest series (part 1,,,,part 2,,,,part 3)I've worked my way through some approaches to creating special image effects with MATLAB. I started easy, and increased the difficulty level as I progressed. In this fourth post, I'm going to create the zebra image above. You might guess that doing so will entail segmenting the zebra--certainly the most difficult part of this problem.

I previously wrote that I can通常在颜色图像的灰度表示或更多的灰度表示中,获取一个良好的分割掩码。当我坚持这一说法时,我也会说有时颜色does提供良好的信息来创建细分面罩。实际上,我将使用颜色在此图像中分割斑马,然后使用手动掩蔽方法(使用imfreehand- mediated approach I used in the previous post) to fine-tune the mask.

按颜色进行细分

使用颜色信息有几种细分方法。例如,如果我想创建单个用户选择的颜色的掩码,我可以使用粉碎区探索一个地区的颜色,否则我可以调用损坏to click-select color samples.

%Note that the |im2double| conversion conveniently scales the intensities to [0,1]URL ='https://blogs.mathworks.com/pick/files/ZebraInField.jpg';img = im2double(imread(url));图,imshow(img);粉碎区

粉碎区provides a convenient tool for exploring the RGB (or grayscale) values underneath a small rectangle; as you drag that rectangle over your image, the intensity values are updated and displayed in a separate figure. In this image, for instance, we might recognize that the grass has an RGB intensity of roughly [0.75 0.65 0.5]. By specifying a "tolerance" of 0.05 (i.e., 5 percent of the color range) we can readily create a mask of "not-zebra" by selecting all pixels that have those approximate red, green, and blue values:

targetColor = [0.75 0.65 0.5]; tolerance = 0.05; mask =。。。img(:,:,1)> = targetColor(1) - 公差&。。。img(:,:,1)<= targetColor(1) +公差&。。。img(:,:,2) >= targetColor(2) - tolerance &。。。img(:,:,2) <= targetColor(2) + tolerance &。。。img(:,:,3) >= targetColor(3) - tolerance &。。。img(::,:,3)<= targetColor(3) +公差;图,imshow(蒙版);标题(“不是斑马?”

Recognize here that each of those constraints on the binary variable "mask" is simply an additional logical AND that I'm applying. I could easily provide different tolerances for ranges above and below each of R, G, and B. (A GUIwith sliders might facilitate that interaction!) I could also create additional masks in a similar manner and combine them, using the logical OR operation, until I achieved the desired segmentation.

Instead of going down that path, though, I'd like to demonstrate another useful approach to color segmentation. In this approach, rather than manually selecting colors on which to base the segmentation mask, I'm going to let MATLAB do the work. The functionrgb2indquantizes an image into a user-specified number of colors. Each of those "quantum levels" can be used to create a unique mask of the image. For instance, here I quantize the zebra into 16 colors and display the binary mask that each index represents:

nColors = 16; X = rgb2ind(img,16);%(x范围从0到ncolors-1)为了II = 0:ncolors-1子图(4,4,ii+1)imshow(ismember(x,ii));标题(sprintf(sprintf)('x =%d',ii));结尾

Now at a glance I can construct a segmentation mask of the zebra, selecting only the indices that have a significant component within the area of interest:

mask = iSmember(x,[0,2,4,5,7,10:12,14,15]);图,imshow(面具)

Clearly, there's a tradeoff between including more indices to "solidify" the zebra mask, and increasing the amount of background "noise" included in the segmentation. I like this as a starting point, so I'm going to use this and start refining:

mask = bwareaopen(mask,100);%Remove small blobsmask = imfill(mask,'holes');%Morphologically fill holes图,imshow(面具)

Normally, I would useimclearborder去除图像顶部的大白色区域,然后使用地区企业,,,,determine the areas of each connected blob. The documentation for地区企业shows how one could easily use that information to eliminate all but the largest object in the resulting image. (That would presumably leave only the zebra.) But in this case, that would also eliminate all the small isolated regions around the zebra's legs, and I would like to keep those. So instead, I'm going to quickly encircle the zebra usingimfreehand,,,,and use the resulting mask to eliminate all peripheral blobs.

manualMask = imfreehand;

posns = getPosition(manualMask); [m,n,~] = size(mask); includeMask = poly2mask(posns(:,1),posns(:,2),m,n); mask = mask & includeMask; mask = imdilate(mask,strel('disk',2));%的轻微调整imshow(mask);

现在使用分段斑马

Yes! I like that mask, and can do a lot with it. Conveniently, the functionroifilt2allows me to operate on only a masked portion of an image. That function, though, only works on 2-D images--not on RGB images. However, when it makes sense to do so, I can modify the red, green, and blue planes indendently, and reconstruct the RGB image usingcat。例如:

processImage {1} = @(x)imadjust(x,[0.05; 0.16],[1.00; 0.11],1.20);红色平面%processImage{2} = @(x) imadjust(x,[0.10; 0.83],[0.00; 1.00], 1.00);%Green planeprocessImage {3} = @(x)imadjust(x,[0.00; 0.22],[1.00; 0.00],1.10);%Blue planer = roifilt2(img(:,:,1),mask,processImage{1}); g = roifilt2(img(:,:,2),mask,processImage{2}); b = roifilt2(img(:,:,3),mask,processImage{3}); enhancedImg = cat(3,r,g,b); figure,imshow(enhancedImg)

Creating the "normal" zebra in the pastel field is a少量trickier, since I can't do a decorrelation stretch plane-by-plane. So instead, I'm going to create the "pastel effect" usingdecorrstretch,,,,and then reset the values在蒙面斑马下面回到他们的原始价值。

imgEnhanced = decorrstretch(img);%Operating on the original image% Now reset the red plane to the masked value of the original imager = imgEnhanced(:,:,1);% Get the enhanced red planetmp = img(:,:,1);% Get the original red plane%Replace the enhanced red under the mask with the original red:r(mask) = tmp(mask);绿色的%相同g = imgEnhanced(:,:,2); tmp = img(:,:,2); g(mask) = tmp(mask);%和蓝色b = imgenhanced(:,:,3);tmp = img(:,:,3);B(掩码)= TMP(掩码);%现在重建增强的图像imgEnhanced = cat(3,r,g,b);% And tweak it with imadjust to intensify colorsimgEnhanced = imadjust(imgEnhanced,[0.1; 0.8],[0.00; 1.00], 1.00);%figure, imshow(imgEnhanced); title('Voila!')%(这将在本文的顶部重现斑马!)

Thanks, a Note on Image Segmentation, and a Challenge

如果你是一个普通的读者“史蒂夫在职业形象cessing," then you'll know that I've commandeered Steve's blog for the past several weeks to present my vision of using MATLAB to create special image effects. I'd like to thank Steve for hosting this guest series; I always enjoy reading Steve's blog, and I'm very pleased to contribute to it.

在这个来宾系列的过程中,我分享了my GUIs调整图像强度和尝试different morphological operations and structuring elements. I previously mentioned that segmentation is often the most difficult part of an image processing problem. So, in the spirit of the coming holidays, here's one more GUI that I'd like to share with you:SegmentTool提供了用于分割图像的交互式环境。它目前包括用于边缘检测,阈值,霍夫变换的选项卡,使用区域和扩展的最小值和最大值以及基于颜色的分割。带它进行测试驱动器,让我知道您的想法,或者您还想看到什么。

这篇文章结束了这个来宾系列,但几乎没有用MATLAB和图像处理工具箱来操纵图像的创意方式。所以我想解决一个挑战:

  • What Effects CanYouCreate with MATLAB? * Share your own cool image effects with us! Show us how, using only MATLAB (and MathWorks Toolboxes, of course) you can create cool image effects. I will catalog, and share on the MATLAB Central File Exchange, useful or fun approaches to creating special effects with images.

Note that it's not just the altered images that I want; please include the code (or at least, pseudo-code, with detail sufficient to allow other users to reproduce the effect). I'll gladly send some swag to anyone who shares an effect that gets included in the special effects gallery. Send your code to me atbrett.shoelson@mathworks.com

Happy MATLABbing!

所有图像版权所有Brett Shoelson;经许可使用。




与Matlab®R2012B一起出版

|
  • 打印
  • 发送电子邮件

댓글

댓글을 남기려면링크를클릭하여하여계정계정하거나계정만드십시오만드십시오만드십시오。