指定区域内图像颜色反转

#include "stdafx.h"
#include <stdio.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;
using namespace std;


Rect selection;
int select_object;
 
/* prototype declaration */
void on_mouse(int event, int x, int y, int flags, void* param);
 
int main(int argc, char *argv[])
{ 
  // (1)load a source image as is
  const char *imagename = argc > 1 ? argv[1] : "../image/fruit.png";
  Mat src_img = imread(imagename, -1);
  if(!src_img.data)
    return -1;
 
  // (2)create a window and set the callback function for mouse events
  namedWindow("Image", 1);
  cvSetMouseCallback("Image", (CvMouseCallback)(&on_mouse), &src_img);
   
  // (3)show the source image with an invert area, and quit when 'esc' pressed
  while(1) {
    Mat dst_img = src_img.clone();
    if(select_object && selection.width > 0 && selection.height > 0) {
      Mat roi(dst_img, selection);
      bitwise_xor(roi, Scalar::all(255), roi);
    }
     
    imshow("Image", dst_img);
    int key = waitKey(10);
    if((char)key==27)
      break;
  }
 
  return 0;
}
 
void on_mouse(int event, int x, int y, int flags, void* param)
{
  static Point2i origin;
  Mat *img = static_cast<Mat*>(param);
 
  // (4)calculate coordinates of selected area (by Click and Drag)
  if(select_object) {
    selection.x = CV_IMIN(x, origin.x);
    selection.y = CV_IMIN(y, origin.y);
    selection.width = selection.x + CV_IABS(x - origin.x);
    selection.height = selection.y + CV_IABS(y - origin.y);
 
    selection.x = CV_IMAX(selection.x, 0);
    selection.y = CV_IMAX(selection.y, 0);
    selection.width = CV_IMIN( selection.width, img->cols );
    selection.height = CV_IMIN( selection.height, img->rows );
    selection.width -= selection.x;
    selection.height -= selection.y;
  }
 
  // (5)process a start and a finish selecting events (i.e. button-up and -down)
  switch(event) {
  case CV_EVENT_LBUTTONDOWN:
    origin = Point2i(x,y);
    selection = Rect(x, y, 0, 0);
    select_object = 1;
    break;
  case CV_EVENT_LBUTTONUP:
    select_object = 0;
    break;
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值