根据OpenCV的像素值调整大小和裁剪图像 [英] OpenCV resizing and cropping image according to pixel value

查看:553
本文介绍了根据OpenCV的像素值调整大小和裁剪图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include "iostream"
#include "cv.h"
#include "highgui.h"
#include "cvaux.h"
#include "cxmisc.h"
#include "math.h"

using namespace cv;
using namespace std;

int main(){

int height, width, x, y, i, minX, minY, maxX, maxY;
char imgFileName[100];


IplImage *origImage = cvLoadImage("BaybayinMark/b9.jpg", -1);
height = origImage->height;
width = origImage->width;

IplImage *grayImage = cvCreateImage(cvSize(width, height), 8, 1);
IplImage *binImage = cvCreateImage(cvSize(width, height), 8, 1);


//Pre-processing phase


cvCvtColor(origImage, grayImage, CV_BGR2GRAY);
cvDilate(grayImage, grayImage, NULL, 1);
cvSmooth(grayImage, grayImage, CV_GAUSSIAN, 21, 21, 0, 0);
cvThreshold(grayImage, binImage, 120, 255, CV_THRESH_BINARY);
cvNormalize(binImage,binImage,0,1,CV_MINMAX);

minX = width;
minY = height;
maxX = 0;
maxY = 0;


CvScalar s;


for (x=0; x<width-1; x++){
for(y=0; y<height-1; y++){
    s = cvGet2D(binImage, y, x);
    //printf("%f\n", s.val[0]);
    if (s.val[0] == 1){
        //printf("HELLO");
        minX = min(minX, x);
        minY = min(minY, y);
        maxX = max(maxX, x);
        maxY = max(maxY, y);

    }   
}
}

cvSetImageROI(binImage, cvRect(minX, minY, maxX-minX, maxY-minY));

IplImage *cropImage = cvCreateImage(cvGetSize(binImage), 8, 1);

cvCopy(binImage, cropImage, NULL);

cvSaveImage("crop/cropImage9.jpg", cropImage);
cvResetImageROI(binImage);

cvReleaseImage(&origImage);
cvReleaseImage(&binImage);

cvReleaseImage(&grayImage);
cvReleaseImage(&cropImage);

}

嗨!我只想问这个code。我试图识别图像的最外缘,并根据作物他们的形象。所有我有运行之后是具有相同大小的黑色图像。难道我尝试做了错误的方式?请赐教我跟OpenCV的初学者。

Hi! i just want to ask about this code. I am trying to identify the outermost edges of an image and crop the image according them. All I was having after running was a black image with the same size. Am I trying to do it the wrong way? Please enlighten me I'm a beginner with OpenCV.

推荐答案

在发现的抢 什么 -the - 赫克,是最问题的人往往会忘记一个更重要的问题: 如何 -the-赫克-DO-我找到的最问题

In the rush of finding what-the-heck-is-the-problem people tend to forget a more important question: how-the-heck-do-i-find-the-problem.

使用图像处理应用中,如何可以通过的 OpenCV中穷人的调试器这是加入 cvSaveImage()呼吁通过code 以能够想象什么样的每一步在做:

With image processing applications, the how can be answered by the poor man's debugger in OpenCV, which is adding cvSaveImage() calls through the code to be able to visualize what every step of the way is doing:

    //Pre-processing phase
    cvCvtColor(origImage, grayImage, CV_BGR2GRAY);
    cvSaveImage("cv_color.jpg", grayImage);

    cvDilate(grayImage, grayImage, NULL, 1);
    cvSaveImage("cv_dilate.jpg", grayImage);

    cvSmooth(grayImage, grayImage, CV_GAUSSIAN, 21, 21, 0, 0);
    cvSaveImage("cv_smooth.jpg", grayImage);

    cvThreshold(grayImage, binImage, 120, 255, CV_THRESH_BINARY);
    cvSaveImage("cv_threshold.jpg", binImage);

    cvNormalize(binImage,binImage,0,1,CV_MINMAX);
    cvSaveImage("cv_normalize.jpg", binImage);

这code表明,生成的图像,甚至您的自定义循环之前获取的黑色,这是负责该呼叫 cvNormalize()。但是,它是有道理的吧?您正在转换这是在以0和1的值的范围在[0..255]像素。

This code reveals that the resulting image gets black even before your custom for loop, and the call that is responsible for that is cvNormalize(). But it makes sense right? You are converting pixels which are in the range [0..255] to values of 0 and 1.

所以问题是,在你处理结束,当你得到的图像保存到dislk,您忘了标准化值回原来的范围

So the problem is that at the end of your processing, when you save the resulting image to the dislk, you forgot to normalize the values back to the original range:

    IplImage *cropImage = cvCreateImage(cvGetSize(binImage), 8, 1);
    cvCopy(binImage, cropImage, NULL);

    cvNormalize(cropImage, cropImage, 0, 255, CV_MINMAX);

    cvSaveImage("result.jpg", cropImage);

和解决该问题。

这篇关于根据OpenCV的像素值调整大小和裁剪图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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