java opencv 阀值分割_OpenCV 阈值分割cvThreshold()

本文档展示了如何在Java环境下利用OpenCV库进行颜色空间转换和阈值分割。通过读取图片,将RGB图像拆分成红色、绿色和蓝色通道,然后将这三个通道的值相加并截断,最后进行二值化处理,实现图像的阈值分割。代码中包含了图像显示和用户交互,便于观察和理解效果。
摘要由CSDN通过智能技术生成

#include "stdafx.h"

#include

#include

#include

void sum_rgb( IplImage* src, IplImage* dst )

{

// Allocate individual image planes.

IplImage* r = cvCreateImage( cvGetSize(src), IPL_DEPTH_8U, 1 );

IplImage* g = cvCreateImage( cvGetSize(src), IPL_DEPTH_8U, 1 );

IplImage* b = cvCreateImage( cvGetSize(src), IPL_DEPTH_8U, 1 );

// Temporary storage.

IplImage* s = cvCreateImage( cvGetSize(src), IPL_DEPTH_8U, 1 );

// Split image onto the color planes.

cvSplit( src, r, g, b, NULL );

// Add equally weighted rgb values.

cvAddWeighted( r, 1./3., g, 1./3., 0.0, s );

cvAddWeighted( s, 1.0, b, 1./3., 0.0, s );

// Truncate values above 100.

cvThreshold( s, dst, 100, 255, CV_THRESH_TRUNC );//对大于100的像素值进行截断,大于100则为255,不大于100的为原值

cvReleaseImage( &r );

cvReleaseImage( &g );

cvReleaseImage( &b );

cvReleaseImage( &s );

}

int main(int argc, char** argv)

{

const char *pchar="E://图片素材//Desert.jpg";

// Create a named window with a the name of the file.

cvNamedWindow( pchar, 1 );

// Load the image from the given file name.

IplImage* src = cvLoadImage(pchar );

IplImage* r = cvCreateImage( cvGetSize(src), IPL_DEPTH_8U, 1 );

IplImage* g = cvCreateImage( cvGetSize(src), IPL_DEPTH_8U, 1 );

IplImage* b = cvCreateImage( cvGetSize(src), IPL_DEPTH_8U, 1 );

// Split image onto the color planes.

cvSplit( src, r, g, b, NULL );

cvShowImage("r",r);

cvShowImage("g",g);

cvShowImage("b",b);

IplImage* dst = cvCreateImage( cvGetSize(src), src->depth, 1);

sum_rgb( src, dst);

cvThreshold( dst, dst, 200, 255, CV_THRESH_BINARY );

// Show the image in the named window

cvShowImage( pchar, dst );

// Idle until the user hits the "Esc" key.

while( 1 )

{

if( (cvWaitKey( 10 )&0x7f) == 27 )

break;

}

// Clean up and don’t be piggies

cvDestroyWindow( pchar);

cvReleaseImage( &src );

cvReleaseImage( &dst );

cvReleaseImage(&r);

cvReleaseImage(&g);

cvReleaseImage(&b);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值