曲率滤波的简单实现只有源码

FIV_RET  fiv_image_curved_filter(fiv_mat* dst_image, fiv_mat* src_image)
{
	FIV_RET ret = FIV_RET_OK;
	if (dst_image == NULL || src_image == NULL
		|| dst_image->color_space != src_image->color_space
		|| dst_image->color_space != FIV_GRAY){
		return FIV_RET_ERR_PARA;
	}

	{
		int i;
		int width, height, stride;
		//创建一张临时的图像作为中间缓存
		fiv_mat* ptr_image_t = fiv_create_image_from_image(src_image);

		width = src_image->width;
		height = src_image->height;
		stride = src_image->stride;

		//执行数据的copy
		fiv_copy_matrix_data_from_matrix(ptr_image_t, src_image);
		fiv_copy_matrix_data_from_matrix(dst_image, src_image);
		//迭代10次
		for (i = 0; i < 10; i++) {

			iv8u* src = ptr_image_t->data.ptr8u;
			iv8u* dst = dst_image->data.ptr8u;

			gray_image_curvature_filter(dst, width, height, stride, src, 1, 1);

			src = dst_image->data.ptr8u;
			dst = ptr_image_t->data.ptr8u;
			//执行数据的copy
			fiv_copy_matrix_data_from_matrix(ptr_image_t, dst_image);

			gray_image_curvature_filter(dst, width, height, stride, src, 2, 1);

			src = ptr_image_t->data.ptr8u;
			dst = dst_image->data.ptr8u;

			//执行数据的copy
			fiv_copy_matrix_data_from_matrix(dst_image, ptr_image_t);

			gray_image_curvature_filter(dst, width, height, stride, src, 1, 2);

			src = dst_image->data.ptr8u;
			dst = ptr_image_t->data.ptr8u;
			//执行数据的copy
			fiv_copy_matrix_data_from_matrix(ptr_image_t, dst_image);

			gray_image_curvature_filter(dst, width, height, stride, src, 2, 2);

			//执行数据的copy
			fiv_copy_matrix_data_from_matrix(dst_image, ptr_image_t);


		}



		fiv_release_matrix(&ptr_image_t);





	}





	return ret;
}



static void gray_image_curvature_filter(iv8u* dst, int width, int height, int stride,
	                                    iv8u* src, int start_rows, int start_cols)
{

	int i, j;
	for (i = start_rows; i < height; i += 2){
		iv8u* ptr_src_i_00 = src + i       * stride;
		iv8u* ptr_src_i_n1 = src + (i - 1) * stride;
		iv8u* ptr_src_i_p1 = src + (i + 1) * stride;
		iv8u* ptr_dst = dst + i * stride;
		for (j = start_cols; j < width; j += 2){
			int t = ptr_src_i_00[j];
			short d8[8],d8_t[8];
			int index;
			__m128i m_d_abs, m_d8;
			 d8[0] = ((ptr_src_i_n1[j] + ptr_src_i_p1[j]) >> 1) - t;
			 d8[1] = ((ptr_src_i_00[j - 1] + ptr_src_i_00[j + 1]) >> 1) - t;
			 d8[2] = ((ptr_src_i_n1[j - 1] + ptr_src_i_p1[j + 1]) >> 1) - t;
			 d8[3] = ((ptr_src_i_n1[j + 1] + ptr_src_i_p1[j - 1]) >> 1) - t;

			 d8[4] = (ptr_src_i_n1[j] + ptr_src_i_00[j - 1] + ptr_src_i_n1[j - 1]) / 3 - t;
			 d8[5] = (ptr_src_i_n1[j] + ptr_src_i_00[j + 1] + ptr_src_i_n1[j + 1]) / 3 - t;

			 d8[6] = (ptr_src_i_00[j - 1] + ptr_src_i_p1[j] + ptr_src_i_p1[j - 1]) / 3 - t;
			 d8[7] = (ptr_src_i_00[j + 1] + ptr_src_i_p1[j] + ptr_src_i_p1[j + 1]) / 3 - t;

			 //d8[4] = (ptr_src_i_n1[j] + ptr_src_i_00[j - 1] - ptr_src_i_n1[j - 1])  - t;
			 //d8[5] = (ptr_src_i_n1[j] + ptr_src_i_00[j + 1] - ptr_src_i_n1[j + 1])  - t;

			 //d8[6] = (ptr_src_i_00[j - 1] + ptr_src_i_p1[j] - ptr_src_i_p1[j - 1])  - t;
			 //d8[7] = (ptr_src_i_00[j + 1] + ptr_src_i_p1[j] - ptr_src_i_p1[j + 1])  - t;

			 m_d8 = _mm_loadu_si128((const __m128i*)d8);
			 m_d_abs =  _mm_abs_epi16(m_d8);
			 m_d_abs =  _mm_minpos_epu16(m_d_abs);
			 _mm_storeu_si128((__m128i*)d8_t, m_d_abs);

			 index = d8_t[1];
			 t = ptr_dst[j];
			 t += d8[index];

			 t = FIV_CLAMP0255(t);

			 ptr_dst[j] = t;

		}
	}



}

你可能感兴趣的:(图像处理,代码优化)