使用openCv c ++复制图像的一部分 [英] copy a part of image with openCv c++

查看:931
本文介绍了使用openCv c ++复制图像的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用opencv,我想从另一个图像的一部分创建一个图像。

I am using opencv and I want to create an image from a part of another image.

我没有找到这样做的功能所以我尝试实现我的想法,包括逐像素复制图像,但是徒劳无功,我没有得到我正在等待的结果。

I didn't find a function that do that so I try to implement my Idea which consist of copying the image pixel by pixel but in vain I didn't get the result I am waiting for.

任何人都有另一个想法

代码:

#include "cv.h"
#include "highgui.h"
#include <stdlib.h>
#include <stdio.h>
#include <iostream>

int main(int argc,char** argv) {
  IplImage * img =0;

  uchar *data;
  int i,j,k;
  int height,width,widthStep,nChannels;
  img=cvLoadImage(argv[1],3);
  height =img->height;
  width = img->width;
  widthStep= img->widthStep;
  nChannels = img->nChannels;
  data=(uchar*)img->imageData;
  IplImage* img1=cvCreateImage(cvSize(height/2,width/2),IPL_DEPTH_8U,nChannels);
  for(i=0;i<height/2;i++){
    for(j=0;j<width/2;j++){
      for(k=0;k<3;k++){
        img1->imageData[i*widthStep+j*nChannels]=data[i*widthStep+j*nChannels];
      }
    }
  }
  cvShowImage("image_Originale2",img1);
  cvWaitKey(0);
  cvReleaseImage(&img);
  return 0;
}


推荐答案

你想要完成什么可以通过在该图像上设置 ROI (感兴趣区域)并将ROI定义的部分复制到新图像来完成。

What you are trying to accomplish can be done by setting a ROI (Region of Interest) on that image and copying that portion defined by the ROI to a new image.

您可以使用<$ c $查看演示c> IplImage 在这篇文章中

You can see a demo using IplImage on this post.

这些帖子显示了使用ROI来解决不同的场景:

These posts show uses of ROI to solve different scenarios:

  • MultiCrops in same image
  • Setting ROI with mouse from a rectangle on a video
  • Put Image in contour (OpenCV)
  • IplImage inside IplImage

这很重要请注意您的代码使用的是OpenCV的 C接口。 C ++接口提供 cv :: Mat ,相当于 IplImage 。换句话说,您正在寻找的是问题的 C解决方案

It's important to note that your code is using the C interface of OpenCV. The C++ interface offers cv::Mat, which is the equivalent of IplImage. In other words, what you are looking for is a C solution to the problem.

这篇关于使用openCv c ++复制图像的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆