opencv imwrite保存图像不成功的问题

gussie 2018-03-07 01:17:21
问题描述:
1.用c++写了一个动态库,功能是:在图片上画直线;coding:
int __stdcall img_line(char* sImagePath, char* rImagePath, int startx, int starty, int endx, int endy, int thickness, int bgr_fst, int bgr_sec, int bgr_thr) {
Mat imgs = imread(sImagePath);
Point start = Point(startx, starty);
Point end = Point(endx, endy);
if(!imgs.empty()){
try{

line(imgs, start, end, Scalar(bgr_fst, bgr_sec, bgr_thr), thickness);
//imshow("处理后图片", imgs);
imwrite(rImagePath, imgs);

}catch (runtime_error& ex) {
fprintf(stderr, "Exception converting image to PNG format: %s\n", ex.what());
return 1;
}
}
// 等待操作,关闭显示
waitKey(0);
return 0;
}
2.vb.net 调用这个函数:
Private Declare Function img_line Lib "VbCallOpenCv.dll" (ByVal sImagePath As String, ByVal startx As Long, ByVal starty As Long, ByVal endx As Long, ByVal endy As Long, ByVal thickness As Integer, ByVal bgr_fst As Long, ByVal bgr_sec As Long, ByVal bgr_thr As Long) As Long

Private Sub Drawline_Click(sender As Object, e As EventArgs) Handles drawline.Click
Dim images As String = pic_ath.Text ‘图片加载路径
Dim startx As Long = X_start.Text ’画线开始坐标x
Dim starty As Long = Y_start.Text ’画线开始坐标y
Dim endx As Long = X_end.Text ’画线终点坐标x
Dim endy As Long = Y_end.Text ’画线终点坐标y
Dim thickness As Long = 2 '线形
Dim bgr_fst As Long = tdf.Text '颜色B通道
Dim bgr_sec As Long = tds.Text '颜色G通道
Dim bgr_thr As Long = tdt.Text '颜色R通道

img_line(images, startx, starty, endx, endy, thickness, bgr_fst, bgr_sec, bgr_thr)
PicLine.Image = Image.FromFile(images)
End Sub
3.当第一次执行画线时可以保存成功!点击按钮画第二根直线时imshow()可以显示图片,但是imwrite()并没有执行,图像没有保存,这是什么原因?用c++测试调用img_line()没有问题。
请大侠们指点,是哪出的问题
...全文
4421 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
明泽. 2021-05-24
  • 打赏
  • 举报
回复
图片显示没问题
明泽. 2021-05-24
  • 打赏
  • 举报
回复
image = imread("C:\\Users\\pc\\Desktop\\images\\2.jpg"); cv::imwrite("F:\\‪4.jpg", image); 我这直接读图,存图都保存不了
hlhfootball 2019-09-26
  • 打赏
  • 举报
回复
int main(int, char *argv[])
{
Mat in_image, out_image;

//读取原始图像
in_image = imread(argv[1], IMREAD_UNCHANGED);
if (in_image.empty()) {
//检查是否读取图像
cout << "Error! Input image cannot be read...\n";
return -1;
}
//创建两个具有图像名称的窗口
namedWindow(argv[1], WINDOW_NORMAL);
//namedWindow(argv[2], WINDOW_NORMAL);
//在之前创建的窗口中显示图片
imshow(argv[1], in_image);
cvtColor(in_image, out_image, CV_BGR2GRAY);
cout << "Press any key to exit...\n";
waitKey(); // Wait for key press
//写入图像
imwrite(argv[2], out_image);
//imshow(argv[2], out_image);
return 0;
}

我的代码如上,编译执行正确,但是保存的图片0kb。
我开始的写法是在转换颜色后显示out_image,考虑到要执行imwrite就没有显示out_image了,但还是不行。
有人知道原因吗。
gussie 2018-05-16
  • 打赏
  • 举报
回复
已经解决,是保存的文件被占用了,没法覆盖造成的,结帖
赵4老师 2018-03-09
  • 打赏
  • 举报
回复
将opencv*.dll拷贝一份放在和exe相同文件夹下。
Jason_er 2018-03-08
  • 打赏
  • 举报
回复
有没有报错呢?一步一步调试看有没有执行imwrite()。

64,282

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧