本文主要介绍关于snake cvSnakeImage,升级改进 opencv 3.0的知识点,对【图像处理之其他杂项(三)之cvSnakeImage改进升级兼容 适用于opencv2,,在opencv3.0以上版本中测试通过】和【双运放】有兴趣的朋友可以看下由【Coming_is_winter】投稿的技术文章,希望该技术和经验能帮到你解决你所遇的【# 图像处理其他杂项】相关技术问题。
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// Intel License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of Intel Corporation may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#include "cv.h"
#define _CV_SNAKE_BIG 2.e+38f
#define _CV_SNAKE_IMAGE 1
#define _CV_SNAKE_GRAD 2
#define CV_VALUE 30
/*F///
// Name: icvSnake8uC1R
// Purpose:
// Context:
// Parameters:
// src - source image,
// srcStep - its step in bytes,
// roi - size of ROI,
// pt - pointer to snake points array
// n - size of points array,
// alpha - pointer to coefficient of continuity energy,
// beta - pointer to coefficient of curvature energy,
// gamma - pointer to coefficient of image energy,
// coeffUsage - if CV_VALUE - alpha, beta, gamma point to single value
// if CV_MATAY - point to arrays
// criteria - termination criteria.
// scheme - image energy scheme
// if _CV_SNAKE_IMAGE - image intensity is energy
// if _CV_SNAKE_GRAD - magnitude of gradient is energy
// Returns:
//F*/
int icvSnake8uC1R(unsigned char *src, //原始图像数据
int srcStep, //每行的字节数
CvSize roi, //图像尺寸
CvPoint * pt, //轮廓点(变形对象)
int n, //轮廓点的个数
float *alpha, //指向α的指针,α可以是单个值,也可以是与轮廓点个数一致的数组
float *beta, //β的值,同α
float *gamma, //γ的值,同α
int coeffUsage, //确定αβγ是用作单个值还是个数组
CvSize win, //每个点用于搜索的最小的领域大小,宽度为奇数
CvTermCriteria criteria, //递归迭代终止的条件准则
int scheme) //确定图像能量场的数据选择,1为灰度,2为灰度梯度
{
int i, j, k;
int neighbors = win.height * win.width; //当前点领域中点的个数
//当前点的位置
int centerx = win.width >> 1;
int centery = win.height >> 1;
float invn; //n 的倒数?
int iteration = 0; //迭代次数
int cOnverged= 0; //收敛标志,0为非收敛
//能量
float *Econt; //
float *Ecurv; //轮廓曲线能量
float *Eimg; //图像能量
float *E; //
//αβγ的副本
float _alpha, _beta, _gamma;
/*#ifdef GRAD_SNAKE */
float *gradient = NULL;
uchar *map = NULL;
int map_width = ((roi.width - 1) >> 3) + 1;
int map_height = ((roi.height - 1) >> 3) + 1;
//CvSepFilter pX, pY;
#define WTILE_SIZE 8
#define TILE_SIZE (WTILE_SIZE + 2)
short dx[TILE_SIZE*TILE_SIZE], dy[TILE_SIZE*TILE_SIZE];
CvMat _dx = cvMat(TILE_SIZE, TILE_SIZE, CV_16SC1, dx);
CvMat _dy = cvMat(TILE_SIZE, TILE_SIZE, CV_16SC1, dy);
CvMat _src = cvMat(roi.height, roi.width, CV_8UC1, src);
/* inner buffer of convolution process */
//char ConvBuffer[400];
/*#endif */
//检点参数的合理性
/* check bad arguments */
//if (src == NULL)
// return CV_NULLPTR_ERR;
//if ((roi.height <= 0) || (roi.width <= 0))
// return CV_BADSIZE_ERR;
//if (srcStep > 1);
int right = MIN(roi.width - 1 - pt[i].x, win.width >> 1);
int upper = MIN(pt[i].y, win.height >> 1);
int bottom = MIN(roi.height - 1 - pt[i].y, win.height >> 1);
//初始化Econt
maxEcOnt= 0;
minEcOnt= _CV_SNAKE_BIG;
//在合理的搜索范围内进行Econt的计算
for (j = -upper; j <= bottom; j++)
{
for (k = -left; k <= right; k++)
{
int diffx, diffy;
float energy;
//在轮廓点集的首尾相接处作相应处理,求轮廓点差分
if (i == 0)
{
diffx = pt[n - 1].x - (pt[i].x + k);
diffy = pt[n - 1].y - (pt[i].y + j);
}
else
//在其他地方作一般处理
{
diffx = pt[i - 1].x - (pt[i].x + k);
diffy = pt[i - 1].y - (pt[i].y + j);
}
//将邻域陈列坐标转成Econt数组的下标序号,计算邻域中每点的Econt
//Econt的值等于平均距离和此点和上一点的距离的差的绝对值(这是怎么来的?)
Econt[(j + centery) * win.width + k + centerx] = energy =
(float)fabs(ave_d -
cvSqrt((float)(diffx * diffx + diffy * diffy)));
//求出所有邻域点中的Econt的最大值和最小值
maxEcOnt= MAX(maxEcont, energy);
minEcOnt= MIN(minEcont, energy);
}
}
//求出邻域点中最大值和最小值之差,并对所有的邻域点的Econt进行标准归一化,若最大值最小
//相等,则邻域中的点Econt全相等,Econt归一化束缚为0
tmp = maxEcont - minEcont;
tmp = (tmp == 0) ? 0 : (1 / tmp);
for (k = 0; k = criteria.max_iter))
cOnverged= 1;
//到大相应精度时,停止迭代(与第一个条件有相同效果)
if ((criteria.type & CV_TERMCRIT_EPS) && (moved <= criteria.epsilon))
cOnverged= 1;
}
//释放各个缓冲区
cvFree(&Econt);
cvFree(&Ecurv);
cvFree(&Eimg);
cvFree(&E);
if (scheme == _CV_SNAKE_GRAD)
{
cvFree(&gradient);
cvFree(&map);
}
return 1;
}
void cvSnakeImage(const IplImage* src, CvPoint* points,
int length, float *alpha,
float *beta, float *gamma,
int coeffUsage, CvSize win,
CvTermCriteria criteria, int calcGradient)
{
CV_FUNCNAME("cvSnakeImage");
//__BEGIN__;
uchar *data;
CvSize size;
int step;
//if (src->nChannels != 1)
//CV_ERROR(CV_BadNumChannels, "input image has more than one channel");
//if (src->depth != IPL_DEPTH_8U)
//CV_ERROR(CV_BadDepth, cvUnsupportedFormat);
cvGetRawData(src, &data, &step, &size);
icvSnake8uC1R(data, step, size, points, length,
alpha, beta, gamma, coeffUsage, win, criteria,
calcGradient ? _CV_SNAKE_GRAD : _CV_SNAKE_IMAGE);
//__END__;
}
/* end of file */
//测试应用程序
//#include "stdafx.h"
#include
#include
#include
#include
#include
#include
IplImage *image = 0; //原始图像 IplImage *image2 = 0; //原始图像copy using namespace std; int Thresholdness = 41; int ialpha = 20; int ibeta = 20; int igamma = 20; void onChange(int pos) { if (image2) cvReleaseImage(&image2); if (image) cvReleaseImage(&image); image2 = cvLoadImage("E://素材//sfsd.jpg", 1); //显示图片 image = cvLoadImage("E://素材//sfsd.jpg", 0); cvThreshold(image, image, Thresholdness, 205, CV_THRESH_BINARY); //分割域值 CvMemStorage* storage = cvCreateMemStorage(0); CvSeq* cOntours= 0; cvFindContours(image, storage, &contours, sizeof(CvContour), //寻找初始化轮廓 CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE); if (!contours) return; int length = contours->total; //cout <
?图1 ?测试效果图 图2 ?测试原图
本文《图像处理之其他杂项(三)之cvSnakeImage改进升级兼容 适用于opencv2,,在opencv3.0以上版本中测试通过》版权归Coming_is_winter所有,引用图像处理之其他杂项(三)之cvSnakeImage改进升级兼容 适用于opencv2,,在opencv3.0以上版本中测试通过需遵循CC 4.0 BY-SA版权协议。