对比度—图像分割的区域对比度计算"/>
Cython—基于区域的对比度—图像分割的区域对比度计算
文章目录
- 代码一
- 代码二
- 测试代码
这是基于区域的对比度的显著性检测代码, 【关于原文请查看】
关于图像分割的区域对比度计算:
- 计算区域的中心与质心。关于计算前的分割区域
- 区域颜色相似度距离的高斯权重分布
- 生成新区域
- 统计区域边界的像素数量
- 形成分割区域
开发环境win10,Visual studio 2015
代码一
#%%cython --cplus --annotate
import numpy as np
cimport cython
cimport numpy as np
from cpython cimport array
from libc.math cimport pow
from libc.math cimport sqrt
from libc.math cimport exp
from libc.math cimport fabs
from libcpp.vector cimport vector
from scipy.spatial.distance import pdist,squareform@cython.boundscheck(False)
@cython.wraparound(False)
cpdef Build_Regions_Contrast(int regNum, np.ndarray[np.int32_t, ndim=2] regIdx1i,int[:,:] colorIdx1i, float[:,:,:]color3fv, float sigmaDist,float ratio,float thr):cdef:int height = regIdx1i.shape[0]int width = regIdx1i.shape[1]int colorNum = color3fv.shape[1]float cx = <float>width / 2.0float cy = <float>height / 2.0Py_ssize_t x = 0, y = 0, i=0, j=0, m=0, n=0, yi=0, xi=0,iii=0pixNum_np = np.zeros(regNum,dtype=np.int64)pixNum_np = np.bincount(regIdx1i.reshape(1, width * height)[0])ybNo_np = np.zeros(regNum, dtype=np.float64)regs_mX = np.zeros(regNum, dtype=np.float64)regs_mY = np.zeros(regNum, dtype=np.float64)regs_vX = np.zeros(regNum, dtype=np.float64)regs_vY = np.zeros(regNum, dtype=np.float64)freIdx_f64 = np.zeros((regNum, colorNum), dtype=np.float64)regColor_np = np.zeros((regNum, colorNum), dtype=np.int32)tile_pixNum = np.zeros((regNum, colorNum), dtype=np.int64)regs_np = np.zeros((regNum, 4), dtype=np.float64)cdef int [:,::1]regColor_view = regColor_npcdef double[:,::1]regs_view = regs_npwith nogil:for y in range(height):for x in range(width):regs_view[regIdx1i[y, x], 0] = fabs(x - cx) # ad2c_0regs_view[regIdx1i[y, x], 1] = fabs(y - cy) # ad2c_1regs_view[regIdx1i[y, x], 2] += x # region center x coordinateregs_view[regIdx1i[y, x], 3] += y # region center y coordinateregColor_view[regIdx1i[y, x], colorIdx1i[y, x]] += 1regs_np[:, 0] = np.divide(regs_np[:, 0], pixNum_np * width)regs_np[:, 1] = np.divide(regs_np[:, 1], pixNum_np * height)regs_mX = np.divide(regs_np[:, 2], pixNum_np)regs_mY = np.divide(regs_np[:, 3], pixNum_np)regs_np[:, 2] = np.divide(regs_mX, width)regs_np[:, 3] = np.divide(regs_mY, height)freIdx_f64 = regColor_np.astype(np.float64)tile_pixNum = np.tile(pixNum_np[:, np.newaxis], (1, colorNum))freIdx_f64 = np.divide(freIdx_f64, tile_pixNum)#==========================================================================================similar_dist = np.zeros((colorNum, colorNum), dtype=np.float64)similar_dist = squareform(pdist(color3fv[0]))rDist_np = np.zeros((regNum, regNum), np.float64)regSal1d_np = np.zeros((1, regNum), np.float64)cdef double[:,:]regs_view2 = regs_npcdef double[:]mX_view = regs_mXcdef double[:]mY_view = regs_mYcdef double[::1]vX_view = regs_vXcdef double[::1]vY_view = regs_vYcdef double[:,:]similar_dist_view = similar_distcdef double[:,::1]rDist_view = rDist_npcdef double[:,::1]regSal1d_view = regSal1d_npcdef double[:,:]freIdx_f64_view = freIdx_f64cdef long long[:]pixNum_view = pixNum_npcdef double dd_np &#
更多推荐
Cython—基于区域的对比度—图像分割的区域对比度计算
发布评论