Adaptive thresholding using the integral image

Adaptive thresholding using the integral image#include #include #include#includeusingnamespacestd; voidvvAdaptiveThreshold(IplImage*inImg,IplImage*outImg){intS=inImg->width>>5;intT=10;char*input,*bin;

大家好,又见面了,我是你们的朋友全栈君。#include <opencv2/opencv.hpp>  

#include <opencv2/legacy/compat.hpp> 

#include <opencv2/imgproc/types_c.h>

#include <fstream>

using namespace std;  

void vvAdaptiveThreshold( IplImage* inImg, IplImage* outImg)

{


int S = inImg->width >> 5;


int T = 10;


char *input, *bin;


input = inImg->imageData;


bin = outImg->imageData;


int width = inImg->width;


int height = inImg->height;


unsigned long* integralImg = 0;


int i, j;


long sum=0;


int count=0;


int index;


int x1, y1, x2, y2;


int s2 = S/2;


//bin = new unsigned char[width*height];


// create the integral image


integralImg = (unsigned long*)malloc(width*height*sizeof(unsigned long*));


for (i=0; i<width; i++)


{


// reset this column sum


sum = 0;


for (j=0; j<height; j++)


{


index = j*width+i;


sum += input[index];


if (i==0)


integralImg[index] = sum;


else


integralImg[index] = integralImg[index-1] + sum;


}


}


// perform thresholding


for (i=0; i<width; i++)


{


for (j=0; j<height; j++)


{


index = j*width+i;


// set the SxS region


x1=i-s2; x2=i+s2;


y1=j-s2; y2=j+s2;


// check the border


if (x1 < 0) x1 = 0;


if (x2 >= width) x2 = width-1;


if (y1 < 0) y1 = 0;


if (y2 >= height) y2 = height-1;


count = (x2-x1)*(y2-y1);


// I(x,y)=s(x2,y2)-s(x1,y2)-s(x2,y1)+s(x1,x1)


sum = integralImg[y2*width+x2] –


integralImg[y1*width+x2] –


integralImg[y2*width+x1] +


integralImg[y1*width+x1];


if ((long)(input[index]*count) < (long)(sum*(100-T)/100))


bin[index] = 255;


else


bin[index] = 0;


}


}


free (integralImg);

}

int main()  //欢迎大家加入图像识别技术交流群:271891601

{

// 从文件中加载原图  

//IplImage *pSrcImage1 = cvLoadImage(“adaptive.jpg”, CV_LOAD_IMAGE_UNCHANGED);  

IplImage *pSrcImage1 = cvLoadImage(“ding9.jpeg”, CV_LOAD_IMAGE_UNCHANGED); 

IplImage *pSrcImage = cvCreateImage(cvGetSize(pSrcImage1), IPL_DEPTH_8U,1);

cvCvtColor(pSrcImage1,pSrcImage,CV_BGR2GRAY);//cvCvtColor(src,des,CV_BGR2GRAY)  

//创建输出的图像

IplImage *pOutImage = cvCreateImage(cvGetSize(pSrcImage), IPL_DEPTH_8U,1);

vvAdaptiveThreshold(pSrcImage,pOutImage);

const char *pstrWindowsATitle = “Adptive Thresholding using the Integral Image”;  

cvNamedWindow(pstrWindowsATitle, CV_WINDOW_AUTOSIZE); 

cvShowImage(pstrWindowsATitle, pOutImage);   

const char *pstrWindowsATitle1 = “origial Image”;  

cvNamedWindow(pstrWindowsATitle1, CV_WINDOW_AUTOSIZE); 

cvShowImage(pstrWindowsATitle1, pSrcImage1);   

cvWaitKey(0); 

//这里记得释放掉不用的资源哦,由于这是测试程序,就不写了,正式的程序一定要写上哦

return 0;

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/145877.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • C++ 字符串转时间 与 时间转转字符串[通俗易懂]

    C++ 字符串转时间 与 时间转转字符串[通俗易懂]1、常用的时间存储方式1)time_t类型,这本质上是一个长整数,表示从1970-01-0100:00:00到目前计时时间的秒数,如果需要更精确一点的,可以使用timeval精确到毫秒。2)tm结构,这本质上是一个结构体,里面包含了各时间字段structtm{inttm_sec;/*secondsafterthe…

    2022年6月2日
    318
  • Windows线程漫谈界面线程和工作者线程

    每个系统都有线程,而线程的最重要的作用就是并行处理,提高软件的并发率。针对界面来说,还能提高界面的响应力。线程分为界面线程和工作者线程,界面实际就是一个线程画出来的东西,这个线程维护一个“消息队列”

    2021年12月25日
    42
  • java杀死进程_linux杀死所有进程

    java杀死进程_linux杀死所有进程使用jps命令jps的作用是显示当前系统的java进程情况及进程id。使用命令taskkill/f/pid”1952″后此时就杀死了当前的指定的进程

    2022年9月21日
    1
  • pytest报错_pycharm git使用

    pytest报错_pycharm git使用前言我们每天写完自动化用例后都会提交到git仓库,随着用例的增多,为了保证仓库代码的干净,当有用例新增的时候,我们希望只运行新增的未提交git仓库的用例。pytest-picked插件可以

    2022年8月6日
    8
  • Python快速入门教程【转】「建议收藏」

    Python快速入门教程【转】「建议收藏」第一章Python基础知识1.1介绍1.1.1特点Python是一种面向对象、解释型计算机程序设计语言。语法简洁清晰,强制用空白符作为语句缩进。Python具有丰富和强大的库,又被称为胶

    2022年7月5日
    18
  • PhpSpreadsheet_php标准输入流stdin

    PhpSpreadsheet_php标准输入流stdin实例化对象composer导包”phpoffice/phpspreadsheet”:”^1.11″,$spreadsheet=newSpreadsheet();$sheet=$spreadsheet->getActiveSheet();合并单元格$sheet->mergeCells(‘A1:J1’);设置单元格的内容$spreadsheet->getActiveSheet()->setCellValue(‘A2′,’序号’)-&

    2022年9月17日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号