opencv显示大图,原图大于窗口大小,不缩放

使用opencv显示图像时,原图大于窗口大小时若不对原图进行缩放,只能显示原图对应窗口大小的这一部分,其余部分不可见。实现过程

  • 为窗口创建滑动条,查看原图
  • 将窗口的右边及底部通过rectangle绘制出滑动区域
  • 拖动滑动条时,对原图滑动窗口截图,大小与窗口剩余部分相同,并将其贴附在窗口上

opencv copyto将图像复制到另一张图像指定位置
将第一张图像指定位置裁剪下来,复制到第二张图像指定位置

    Mat src1(500, 500,CV_8UC3, cvScalarAll(0));
    Mat src2(500, 500,CV_8UC3, cvScalarAll(255));
    imshow("src1", src1);
    imshow("src2", src2);
    Mat roi_1 = src1(Rect(50,50,50,50));
    roi_1.copyTo(src2(Rect(50,50,50,50)));

    imshow("dst", src2);

结果:

opencv显示大图,原图大于窗口大小,不缩放_第1张图片
opencv显示大图,原图大于窗口大小,不缩放_第2张图片

全部源码:

double mx = 0, my = 0;
int dx = 0, dy = 0, horizBar_x = 0, vertiBar_y = 0;
bool clickVertiBar = false, clickHorizBar = false, needScroll = false;
CvRect rect_bar_horiz, rect_bar_verti;
void mouse_callback(int event, int x, int y, int flags, void* param)
{
    if (needScroll)
    {
        switch (event)
        {
        case CV_EVENT_LBUTTONDOWN:
            mx = x, my = y;
            dx = 0, dy = 0;
            // 按下左键时光标定位在水平滚动条区域内  
            if (x >= rect_bar_horiz.x && x <= rect_bar_horiz.x + rect_bar_horiz.width
                && y >= rect_bar_horiz.y && y <= rect_bar_horiz.y + rect_bar_horiz.height)
            {
                clickHorizBar = true;
            }
            // 按下左键时光标定位在垂直滚动条区域内  
            if (x >= rect_bar_verti.x && x <= rect_bar_verti.x + rect_bar_verti.width
                && y >= rect_bar_verti.y && y <= rect_bar_verti.y + rect_bar_verti.height)
            {
                clickVertiBar = true;
            }
            break;
        case CV_EVENT_MOUSEMOVE:
            if (clickHorizBar)
            {
                dx = fabs(x - mx) > 1 ? (int)(x - mx) : 0;
                dy = 0;
            }
            if (clickVertiBar)
            {
                dx = 0;
                dy = fabs(y - my) > 1 ? (int)(y - my) : 0;
            }
            mx = x, my = y;
            break;
        case CV_EVENT_LBUTTONUP:
            mx = x, my = y;
            dx = 0, dy = 0;
            clickHorizBar = false;
            clickVertiBar = false;
            break;
        default:
            dx = 0, dy = 0;
            break;
        }
    }
}

void myShowImageScroll(Mat& src_img,
    int winWidth = 1400, int winHeight = 700)
{
    Mat dst_img;
    CvRect  rect_dst,   // 窗口中有效的图像显示区域  
        rect_src;   // 窗口图像对应于源图像中的区域  
    int imgWidth = src_img.cols,
        imgHeight = src_img.rows,
        barWidth = 25;  // 滚动条的宽度(像素)  
    double  scale_w = (double)imgWidth / (double)winWidth,    // 源图像与窗口的宽度比值  用以判断是否超出显示范围
        scale_h = (double)imgHeight / (double)winHeight;            // 源图像与窗口的高度比值  用以判断是否超出显示范围

    if (scale_w < 1)                                                                     //如果小于1 说明原图比窗口小,窗口的宽度将重新赋值
        winWidth = imgWidth + barWidth;
    if (scale_h < 1)                                                                     //如果小于1 说明原图比窗口小,窗口的高度将重新赋值
        winHeight = imgHeight + barWidth;

    int showWidth = winWidth, showHeight = winHeight; // 窗口中有效的图像显示区域的宽和高  
    int src_x = 0, src_y = 0;                                                      // 源图像中 rect_src 的左上角位置  
    int horizBar_width = 0, horizBar_height = 0,                   //定义并初始化垂直于水平滑块的宽高
        vertiBar_width = 0, vertiBar_height = 0;

    needScroll = scale_w > 1.0 || scale_h > 1.0 ? true : false;
    // 若图像大于设定的窗口大小,则显示滚动条  
    if (needScroll)
    {
        dst_img.create(Size(winWidth, winHeight), CV_8UC3);
        // 源图像宽度大于窗口宽度,则显示水平滚动条  
        if (scale_w > 1.0)  //宽度超出了
        {
            showHeight = winHeight - barWidth;
            horizBar_width = (int)((double)winWidth / scale_w);
            horizBar_height = winHeight - showHeight;
            horizBar_x = min(
                max(0, horizBar_x + dx),
                winWidth - horizBar_width);
            rect_bar_horiz = cvRect(
                horizBar_x,
                showHeight + 1,
                horizBar_width,
                horizBar_height);
            // 显示水平滚动条  
            rectangle(dst_img, rect_bar_horiz, Scalar(255,255,255), -1);
        }
        // 源图像高度大于窗口高度,则显示垂直滚动条  
        if (scale_h > 1.0)  //高度超出了
        {
            showWidth = winWidth - barWidth;
            vertiBar_width = winWidth - showWidth;
            vertiBar_height = (int)((double)winHeight / scale_h);
            vertiBar_y = min(
                max(0, vertiBar_y + dy),
                winHeight - vertiBar_height);
            rect_bar_verti = cvRect(
                showWidth + 1,
                vertiBar_y,
                vertiBar_width,
                vertiBar_height);   //确定垂直滚动条的白色部分的大小
            // 显示垂直滚动条  
            rectangle(dst_img, rect_bar_verti, Scalar(255,255,255), -1);
        }

        showWidth = min(showWidth, imgWidth);
        showHeight = min(showHeight, imgHeight);
        // 设置窗口显示区的 ROI  
        rect_dst = cvRect(0, 0, showWidth, showHeight);
        // 设置源图像的 ROI  
        src_x = (int)((double)horizBar_x * scale_w);
        src_y = (int)((double)vertiBar_y * scale_h);
        src_x = min(src_x, imgWidth - showWidth);
        src_y = min(src_y, imgHeight - showHeight);
        rect_src = cvRect(src_x, src_y, showWidth, showHeight);
        Mat roi_1 = src_img(rect_src);
        // 将源图像内容复制到窗口显示区  
        roi_1.copyTo(dst_img(rect_dst));

        // 显示图像和滚动条  
        imshow("src", dst_img);
    }
    // 源图像小于设定窗口,则直接显示图像,无滚动条  
    else
    {
        imshow("src", src_img);
    }
}
int main()
{
    Mat img = imread("1.bmp");
    namedWindow("src", 1);
    setMouseCallback("src", mouse_callback);
    while (1)
    {
        myShowImageScroll(img);
        int KEY = cvWaitKey(10);
        if ((char)KEY == 27)
            break;
    }
    cvDestroyWindow("src");
    return 0;
}

你可能感兴趣的:(计算机视觉,opencv,人工智能)