关于感兴趣区域提取

八领域搜索,水平集分割,分水岭分割,

首先是水平集方法,效果并不好

clear all;
close all;
Img = imread(‘index_1.bmp‘);  % The same cell image in the paper is used here
Img=double(Img(:,:,1));
sigma=1.5;    % scale parameter in Gaussian kernel for smoothing.
G=fspecial(‘gaussian‘,15,sigma);
Img_smooth=conv2(Img,G,‘same‘);  % smooth image by Gaussiin convolution
[Ix,Iy]=gradient(Img_smooth);
f=Ix.^2+Iy.^2;
g=1./(1+f);  % edge indicator function.

epsilon=1.5; % the papramater in the definition of smoothed Dirac function

timestep=5;  % time step
mu=0.2/timestep;  % coefficient of the internal (penalizing) energy term P(\phi)
          % Note: the product timestep*mu must be less than 0.25 for stability!

lambda=5; % coefficient of the weighted length term Lg(\phi)
alf=1.5;  % coefficient of the weighted area term Ag(\phi);
          % Note: Choose a positive(negative) alf if the initial contour is outside(inside) the object.

% define initial level set function (LSF) as -c0, 0, c0 at points outside, on
% the boundary, and inside of a region R, respectively.
[nrow, ncol]=size(Img);
c0=4;
initialLSF=c0*ones(nrow,ncol);
w=8;
initialLSF(w+1:end-w, w+1:end-w)=0;  % zero level set is on the boundary of R.
                                     % Note: this can be commented out. The intial LSF does NOT necessarily need a zero level set.

initialLSF(w+2:end-w-1, w+2: end-w-1)=-c0; % negative constant -c0 inside of R, postive constant c0 outside of R.
u=initialLSF;
figure;imagesc(Img);colormap(gray);hold on;
[c,h] = contour(u,[0 0],‘r‘);
title(‘Initial contour‘);

% start level set evolution
for n=1:300
    u=EVOLUTION(u, g ,lambda, mu, alf, epsilon, timestep, 1);
    if mod(n,20)==0
        pause(0.001);
        imagesc(Img);colormap(gray);hold on;
        [c,h] = contour(u,[0 0],‘r‘);
        iterNum=[num2str(n), ‘ iterations‘];
        title(iterNum);
        hold off;
    end
end
imagesc(Img);colormap(gray);hold on;
[c,h] = contour(u,[0 0],‘r‘);
totalIterNum=[num2str(n), ‘ iterations‘];
title([‘Final contour, ‘, totalIterNum]);

  

function u = EVOLUTION(u0, g, lambda, mu, alf, epsilon, delt, numIter)
%  EVOLUTION(u0, g, lambda, mu, alf, epsilon, delt, numIter) updates the level set function
%  according to the level set evolution equation in Chunming Li et al‘s paper:
%      "Level Set Evolution Without Reinitialization: A New Variational Formulation"
%       in Proceedings CVPR‘2005,
%  Usage:
%   u0: level set function to be updated
%   g: edge indicator function
%   lambda: coefficient of the weighted length term L(\phi)
%   mu: coefficient of the internal (penalizing) energy term P(\phi)
%   alf: coefficient of the weighted area term A(\phi), choose smaller alf
%   epsilon: the papramater in the definition of smooth Dirac function, default value 1.5
%   delt: time step of iteration, see the paper for the selection of time step and mu
%   numIter: number of iterations.
%

u=u0;
[vx,vy]=gradient(g);

for k=1:numIter
    u=NeumannBoundCond(u);
    [ux,uy]=gradient(u);
    normDu=sqrt(ux.^2 + uy.^2 + 1e-10);
    Nx=ux./normDu;
    Ny=uy./normDu;
    diracU=Dirac(u,epsilon);
    K=curvature_central(Nx,Ny);
    weightedLengthTerm=lambda*diracU.*(vx.*Nx + vy.*Ny + g.*K);
    penalizingTerm=mu*(4*del2(u)-K);
    weightedAreaTerm=alf.*diracU.*g;
    u=u+delt*(weightedLengthTerm + weightedAreaTerm + penalizingTerm);  % update the level set function
end

% the following functions are called by the main function EVOLUTION
function f = Dirac(x, sigma)   %水平集狄拉克计算
f=(1/2/sigma)*(1+cos(pi*x/sigma));
b = (x<=sigma) & (x>=-sigma);
f = f.*b;

function K = curvature_central(nx,ny);  %曲率中心
[nxx,junk]=gradient(nx);
[junk,nyy]=gradient(ny);
K=nxx+nyy;

function g = NeumannBoundCond(f)
% Make a function satisfy Neumann boundary condition
[nrow,ncol] = size(f);
g = f;
g([1 nrow],[1 ncol]) = g([3 nrow-2],[3 ncol-2]);
g([1 nrow],2:end-1) = g([3 nrow-2],2:end-1);
g(2:end-1,[1 ncol]) = g(2:end-1,[3 ncol-2]);

  %%下面是八领域搜索算法代码,没搞明白怎么回事,先贴上来吧。。、、

clear all;
close all;
clc;
%外边界
img=imread(‘rice.png‘);
img=img>128;
imshow(img);
[m n]=size(img);

imgn=zeros(m,n);        %边界标记图像
ed=[-1 -1;0 -1;1 -1;1 0;1 1;0 1;-1 1;-1 0]; %从左上角像素判断
for i=2:m-1
    for j=2:n-1
        if img(i,j)==1      %如果当前像素是前景像素

            for k=1:8
                ii=i+ed(k,1);
                jj=j+ed(k,2);
                if img(ii,jj)==0    %当前像素周围如果是背景,边界标记图像相应像素标记
                    imgn(ii,jj)=1;
                end
            end

        end
    end
end

figure;
imshow(imgn,[]);

%不过要是真取二值图像外边界,通常是原图膨胀图减去原图就行了
se = strel(‘square‘,3);
imgn=imdilate(img,se)-img;
figure;
imshow(imgn)

  

时间: 2024-08-05 04:47:34

关于感兴趣区域提取的相关文章

opencv中感兴趣区域以及mask的使用

在图像处理的过程中,我们时常需要对指定区域或目标进行操作,这个区域我们称之为感兴趣区域.在学习opencv的初级阶段,对于感兴趣区域的操作方法是必须要掌握的. 比如下图: 我们获取到一帧图像Img,它里面有两个目标,一个蓝色块和一个红色块,我们一般会经常碰到以下三种情况: (1)我们希望将Img中的蓝色目标提取出来并另存为一个图像: (2)我们希望将Img中不是蓝色目标的其他目标都隐藏起来,只显示蓝色目标,或只对蓝色目标的区域进行图像处理. (3)我们只希望对Img中红色目标区域进行处理. 我们

OpenCV中感兴趣区域的选取与检测(一)

1.感兴趣区域的选取 感兴趣区域(Region of Interest, ROI)的选取,一般有两种情形:1)已知ROI在图像中的位置:2)ROI在图像中的位置未知. 1)第一种情形 很简单,根据ROI的坐标直接从原图抠出,不过前提是要知道其坐标,直接上例子吧. int getROI(Mat image, Rect rect) { Mat img=image.clone(); Mat roi; int cols=img.cols, rows=img.rows; //ROI越界,返回 if(col

【opencv入门之二】感兴趣区域ROI,线性混合addWeighted

参考网站: http://blog.csdn.net/poem_qianmo/article/details/20911629 1.感兴趣区域ROI //[2]定义一个Mat类型并给其设定ROI区域 Mat imageROI = srcImage1( Rect(200, 250, logoImage.cols, logoImage.rows )); //[3]加载掩摸(必须是灰度图) Mat mask = imread( "dota_logo.jpg", 0 ); //[4]将掩摸拷贝

获取图片中感兴趣区域的信息(Matlab实现)

内容提要 如果一幅图中只有一小部分图像你感兴趣(你想研究的部分),那么截图工具就可以了,但是如果你想知道这个区域在原图像中的坐标位置呢? 这可是截图工具所办不到的,前段时间我就需要这个功能,于是将其用Matlab实现. 其实只要用到Matlab中的两个函数: 函数: imrect 函数: getPosition 如果要截取其中的部分图像,就离不开下面的函数: 函数: imcrop 代码实现 clc; clear; close all; %-----------------------------

opencv图像及视频感兴趣区域设置

之前学过的简单绘图方法,例如矩形,椭圆等,今天试着在视频图像中用矩形标注感兴趣区域(ROI) <----图像篇----> #include "opencv2/core/core.hpp" #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/highgui/highgui.hpp" #include <iostream> using namespace std;

OpenCV设置感兴趣区域ROI,对图像进行局部处理

今天终于将霍夫检测的源码用C++写出来了,八天时间遇到最多的问题还是数据结构不熟悉,有些问题不知道怎么问,不知道从何下手 写出来的检测直线基本符合要求,问题是如果在现实中检测铁轨的话,附近会有石子,测试中发现如果石子的数目少,则检测效果较好,如果石子多,那么石子构成的点将影响检测 现在想到的一种方法就是设置感兴趣区域,将石子的影响减少 源码如下: 1.Iplimage设置感兴趣区域 int main( int argc, char** argv ) { IplImage *img = cvLoa

opencv-视频处理--画感兴趣区域(ROI)

感兴趣区域的划分,在视频处理中有着重要应用,用OpenCV介绍两种,在视频中标注感兴趣区域的方法: 原视频: ----------------------------------------------------------------------------------------------------------------------------------- 第一种:暂停视频或者在视频流的第一帧中,画出感兴趣区域 #include<opencv2\core\core.hpp> #i

【练习3.5】使用感兴趣区域(ROI)

2014-05-29 第三章 初探OpenCV 练习第5题: 题目:学习使用感兴趣区域(ROI).创建一个210*210的单通道图像并将其归0.在图中使用ROI和cvSet建立一个增长如金字塔状的数组,也就是:外部边界为0,下一个内部边界应该为20,在下一个内部边界为40,依此类推,直到最后内部值为200,所有边界应该为10个像素宽度.最后显示这个图形. 按照题目要求使用ROI和cvSet实现 #include "stdafx.h" #include "cv.h"

[zt] ROI (Region of Interest) 感兴趣区域 OpenCV

在以前介绍IplImage结构的时候,有一个重要的参数——ROI.ROI全称是”Region Of Interest”,即感兴趣的区域.实际上,它是IPL/IPP(这两个是Inter的库)结构IplROI的实例.IplROI包含xOffset.yOffset.height.width和coi成员变量.其中COI代表channel of interest(感兴趣的通道).ROI的思想是:一旦设定ROI,通常组用于整幅图像的函数便只会对ROI所表示的子图像进行操作.如果COI被设置非0值,则对该图像