保存OpenCV中Mat_<Vec3f>格式的图像

在OpenCV中,有时会出现如Mat_<Vec3f>图像类型,直接用cvsaveImage或者imwrite保存,会出现一片黑色。保存失败:

如下


这是定义格式问题

此时,需要转换正确的格式才能保存成功

OpenCV中有convertTo函数

是一种格式转换函数

具体如下

——————————————————————————————————————————————————

Mat::convertTo
Converts an array to another data type with optional scaling.
C++: void Mat::convertTo(OutputArray m, int rtype, double alpha=1, double beta=0 ) const
Parameters
m – output matrix; if it does not have a proper size or type before the operation, it is reallocated.
rtype – desired output matrix type or, rather, the depth since the number of channels are the
same as the input has; if rtype is negative, the output matrix will have the same type as the
input.
alpha – optional scale factor.
beta – optional delta added to the scaled values.
The method converts source pixel values to the target data type. saturate_cast<> is applied at the end to avoid
possible overflows:
m(x; y) = saturate_cast < rType > ( (this)(x; y) + )

————————————————————分割线——————————————————————————————

Mat::convertTo

在缩放或不缩放的情况下转换为另一种数据类型。

C++:

void Mat::convertTo(OutputArray m,int rtype,double alpha=1,double beta=0)const

参数:

m – 目标矩阵。如果它的尺寸和类型不正确,在操作之前会重新分配。

rtype – 要求是目标矩阵的类型,或者在当前通道数与源矩阵通道数相同的情况下的depth。如果rtype 为负,目标矩阵与源矩阵类型相同。

beta – 可选的delta加到缩放值中去。

该方法将源像素值转化为目标类型saturate_cast<> 要放在最后以避免溢出

m( x;y) = saturate_cast < rType > ( α*( *this)( x;y) +β)

——————————————————————————————————————————————————

具体实现如下:

Mat_<Vec3f> blend = LaplacianBlend(l, r, m);
	imshow("blended",blend);
	Mat re;
	blend.convertTo(re,CV_8UC3,255);
	imwrite("blended.jpg",re);

结果:



Mat_<Vec3f>源自这里


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
抱歉,您提供的代码是使用了OpenCV库的C++代码,而您要求使用C语言实现。在C语言,没有现成的图像处理库,因此需要使用其他第三方库或手动编写图像处理算法来实现。以下是使用C语言手动编写的简单图像处理算法,可以实现对图像进行随机偏移的效果: ```c #include <stdio.h> #include <stdlib.h> #include <time.h> #define BMP_HEADER_SIZE 54 int main(void) { FILE *fp_in, *fp_out; unsigned char header[BMP_HEADER_SIZE]; int width, height, bit_count, line_size, offset, random_num; unsigned char *img_in, *img_out; srand((unsigned)time(NULL)); fp_in = fopen("input.bmp", "rb"); fp_out = fopen("output.bmp", "wb"); if (fp_in == NULL || fp_out == NULL) { printf("Failed to open files!\n"); return 1; } fread(header, sizeof(unsigned char), BMP_HEADER_SIZE, fp_in); width = *(int *)&header[18]; height = *(int *)&header[22]; bit_count = *(short *)&header[28]; line_size = (bit_count * width + 31) / 32 * 4; offset = line_size - bit_count / 8 * width; img_in = (unsigned char *)malloc(sizeof(unsigned char) * height * line_size); img_out = (unsigned char *)malloc(sizeof(unsigned char) * height * line_size); fread(img_in, sizeof(unsigned char), height * line_size, fp_in); for (int y = 0; y < height; ++y) { for (int x = 0; x < width; ++x) { random_num = rand() % 5; img_out[y * line_size + x * bit_count / 8 + 0] = img_in[(y + random_num) * line_size + (x + random_num) * bit_count / 8 + 0]; img_out[y * line_size + x * bit_count / 8 + 1] = img_in[(y + random_num) * line_size + (x + random_num) * bit_count / 8 + 1]; img_out[y * line_size + x * bit_count / 8 + 2] = img_in[(y + random_num) * line_size + (x + random_num) * bit_count / 8 + 2]; } } fwrite(header, sizeof(unsigned char), BMP_HEADER_SIZE, fp_out); fwrite(img_out, sizeof(unsigned char), height * line_size, fp_out); free(img_in); free(img_out); fclose(fp_in); fclose(fp_out); return 0; } ``` 该代码使用了BMP文件格式,可以将图片保存为BMP格式后进行处理。在代码,首先读取BMP文件头信息,然后根据图片的宽度、高度和每像素位数计算出每行像素数据的字节数。接着使用随机数生成器生成每个像素的随机偏移量,然后将原图像的像素进行偏移,输出到新的图像。最后将新的图像保存为BMP格式。 需要注意的是,该算法仅为简单的示例代码,可能存在一些局限性和缺陷。在实际应用,需要根据具体的需求和情况进行优化和改进。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值