java圣诞树_如何检测圣诞树?

2024-02-05 13:20
文章标签 java 检测 圣诞树

本文主要是介绍java圣诞树_如何检测圣诞树?,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

EDIT NOTE: 我编辑了这篇文章,以便(i)按照要求的要求单独处理每个树形图像,(ii)同时考虑对象的亮度和形状,以提高结果的质量 .

下面介绍一种考虑物体亮度和形状的方法 . 换句话说,它寻找具有三角形形状和明显亮度的物体 . 它是使用Marvin图像处理框架在Java中实现的 .

第一步是颜色阈值处理 . 这里的目标是将分析集中在具有显着亮度的物体上 .

output images:

source code:

public class ChristmasTree {

private MarvinImagePlugin fill = MarvinPluginLoader.loadImagePlugin("org.marvinproject.image.fill.boundaryFill");

private MarvinImagePlugin threshold = MarvinPluginLoader.loadImagePlugin("org.marvinproject.image.color.thresholding");

private MarvinImagePlugin invert = MarvinPluginLoader.loadImagePlugin("org.marvinproject.image.color.invert");

private MarvinImagePlugin dilation = MarvinPluginLoader.loadImagePlugin("org.marvinproject.image.morphological.dilation");

public ChristmasTree(){

MarvinImage tree;

// Iterate each image

for(int i=1; i<=6; i++){

tree = MarvinImageIO.loadImage("./res/trees/tree"+i+".png");

// 1. Threshold

threshold.setAttribute("threshold", 200);

threshold.process(tree.clone(), tree);

}

}

public static void main(String[] args) {

new ChristmasTree();

}

}

在第二步中,图像中最亮的点被扩张以形成形状 . 该过程的结果是具有显着亮度的物体的可能形状 . 应用填充填充分段,检测断开的形状 .

output images:

source code:

public class ChristmasTree {

private MarvinImagePlugin fill = MarvinPluginLoader.loadImagePlugin("org.marvinproject.image.fill.boundaryFill");

private MarvinImagePlugin threshold = MarvinPluginLoader.loadImagePlugin("org.marvinproject.image.color.thresholding");

private MarvinImagePlugin invert = MarvinPluginLoader.loadImagePlugin("org.marvinproject.image.color.invert");

private MarvinImagePlugin dilation = MarvinPluginLoader.loadImagePlugin("org.marvinproject.image.morphological.dilation");

public ChristmasTree(){

MarvinImage tree;

// Iterate each image

for(int i=1; i<=6; i++){

tree = MarvinImageIO.loadImage("./res/trees/tree"+i+".png");

// 1. Threshold

threshold.setAttribute("threshold", 200);

threshold.process(tree.clone(), tree);

// 2. Dilate

invert.process(tree.clone(), tree);

tree = MarvinColorModelConverter.rgbToBinary(tree, 127);

MarvinImageIO.saveImage(tree, "./res/trees/new/tree_"+i+"threshold.png");

dilation.setAttribute("matrix", MarvinMath.getTrueMatrix(50, 50));

dilation.process(tree.clone(), tree);

MarvinImageIO.saveImage(tree, "./res/trees/new/tree_"+1+"_dilation.png");

tree = MarvinColorModelConverter.binaryToRgb(tree);

// 3. Segment shapes

MarvinImage trees2 = tree.clone();

fill(tree, trees2);

MarvinImageIO.saveImage(trees2, "./res/trees/new/tree_"+i+"_fill.png");

}

private void fill(MarvinImage imageIn, MarvinImage imageOut){

boolean found;

int color= 0xFFFF0000;

while(true){

found=false;

Outerloop:

for(int y=0; y

for(int x=0; x

if(imageOut.getIntComponent0(x, y) == 0){

fill.setAttribute("x", x);

fill.setAttribute("y", y);

fill.setAttribute("color", color);

fill.setAttribute("threshold", 120);

fill.process(imageIn, imageOut);

color = newColor(color);

found = true;

break Outerloop;

}

}

}

if(!found){

break;

}

}

}

private int newColor(int color){

int red = (color & 0x00FF0000) >> 16;

int green = (color & 0x0000FF00) >> 8;

int blue = (color & 0x000000FF);

if(red <= green && red <= blue){

red+=5;

}

else if(green <= red && green <= blue){

green+=5;

}

else{

blue+=5;

}

return 0xFF000000 + (red << 16) + (green << 8) + blue;

}

public static void main(String[] args) {

new ChristmasTree();

}

}

如输出图像所示,检测到多个形状 . 在这个问题中,图像中只有几个亮点 . 但是,实施此方法是为了处理更复杂的情况 .

在下一步中,分析每个形状 . 一种简单的算法检测具有类似于三角形的图案的形状 . 该算法逐行分析对象形状 . 如果每个形状线的质量的中心几乎相同(给定阈值)并且随着y的增加质量增加,则该对象具有类似三角形的形状 . 形状线的质量是该线中属于该形状的像素数 . 想象一下,您水平切割对象并分析每个水平线段 . 如果它们彼此集中并且长度从第一个段增加到线性模式中的最后一个段,则可能有一个类似于三角形的对象 .

source code:

private int[] detectTrees(MarvinImage image){

HashSet analysed = new HashSet();

boolean found;

while(true){

found = false;

for(int y=0; y

for(int x=0; x

int color = image.getIntColor(x, y);

if(!analysed.contains(color)){

if(isTree(image, color)){

return getObjectRect(image, color);

}

analysed.add(color);

found=true;

}

}

}

if(!found){

break;

}

}

return null;

}

private boolean isTree(MarvinImage image, int color){

int mass[][] = new int[image.getHeight()][2];

int yStart=-1;

int xStart=-1;

for(int y=0; y

int mc = 0;

int xs=-1;

int xe=-1;

for(int x=0; x

if(image.getIntColor(x, y) == color){

mc++;

if(yStart == -1){

yStart=y;

xStart=x;

}

if(xs == -1){

xs = x;

}

if(x > xe){

xe = x;

}

}

}

mass[y][0] = xs;

mass[y][3] = xe;

mass[y][4] = mc;

}

int validLines=0;

for(int y=0; y

if

(

mass[y][5] > 0 &&

Math.abs(((mass[y][0]+mass[y][6])/2)-xStart) <= 50 &&

mass[y][7] >= (mass[yStart][8] + (y-yStart)*0.3) &&

mass[y][9] <= (mass[yStart][10] + (y-yStart)*1.5)

)

{

validLines++;

}

}

if(validLines > 100){

return true;

}

return false;

}

最后,在原始图像中突出显示每个形状的类似于三角形且具有显着亮度的位置(在这种情况下为圣诞树),如下所示 .

final output images:

final source code:

public class ChristmasTree {

private MarvinImagePlugin fill = MarvinPluginLoader.loadImagePlugin("org.marvinproject.image.fill.boundaryFill");

private MarvinImagePlugin threshold = MarvinPluginLoader.loadImagePlugin("org.marvinproject.image.color.thresholding");

private MarvinImagePlugin invert = MarvinPluginLoader.loadImagePlugin("org.marvinproject.image.color.invert");

private MarvinImagePlugin dilation = MarvinPluginLoader.loadImagePlugin("org.marvinproject.image.morphological.dilation");

public ChristmasTree(){

MarvinImage tree;

// Iterate each image

for(int i=1; i<=6; i++){

tree = MarvinImageIO.loadImage("./res/trees/tree"+i+".png");

// 1. Threshold

threshold.setAttribute("threshold", 200);

threshold.process(tree.clone(), tree);

// 2. Dilate

invert.process(tree.clone(), tree);

tree = MarvinColorModelConverter.rgbToBinary(tree, 127);

MarvinImageIO.saveImage(tree, "./res/trees/new/tree_"+i+"threshold.png");

dilation.setAttribute("matrix", MarvinMath.getTrueMatrix(50, 50));

dilation.process(tree.clone(), tree);

MarvinImageIO.saveImage(tree, "./res/trees/new/tree_"+1+"_dilation.png");

tree = MarvinColorModelConverter.binaryToRgb(tree);

// 3. Segment shapes

MarvinImage trees2 = tree.clone();

fill(tree, trees2);

MarvinImageIO.saveImage(trees2, "./res/trees/new/tree_"+i+"_fill.png");

// 4. Detect tree-like shapes

int[] rect = detectTrees(trees2);

// 5. Draw the result

MarvinImage original = MarvinImageIO.loadImage("./res/trees/tree"+i+".png");

drawBoundary(trees2, original, rect);

MarvinImageIO.saveImage(original, "./res/trees/new/tree_"+i+"_out_2.jpg");

}

}

private void drawBoundary(MarvinImage shape, MarvinImage original, int[] rect){

int yLines[] = new int[6];

yLines[0] = rect[1];

yLines[1] = rect[1]+(int)((rect[3]/5));

yLines[2] = rect[1]+((rect[3]/5)*2);

yLines[3] = rect[1]+((rect[3]/5)*3);

yLines[4] = rect[1]+(int)((rect[3]/5)*4);

yLines[5] = rect[1]+rect[3];

List points = new ArrayList();

for(int i=0; i

boolean in=false;

Point startPoint=null;

Point endPoint=null;

for(int x=rect[0]; x

if(shape.getIntColor(x, yLines[i]) != 0xFFFFFFFF){

if(!in){

if(startPoint == null){

startPoint = new Point(x, yLines[i]);

}

}

in = true;

}

else{

if(in){

endPoint = new Point(x, yLines[i]);

}

in = false;

}

}

if(endPoint == null){

endPoint = new Point((rect[0]+rect[2])-1, yLines[i]);

}

points.add(startPoint);

points.add(endPoint);

}

drawLine(points.get(0).x, points.get(0).y, points.get(1).x, points.get(1).y, 15, original);

drawLine(points.get(1).x, points.get(1).y, points.get(3).x, points.get(3).y, 15, original);

drawLine(points.get(3).x, points.get(3).y, points.get(5).x, points.get(5).y, 15, original);

drawLine(points.get(5).x, points.get(5).y, points.get(7).x, points.get(7).y, 15, original);

drawLine(points.get(7).x, points.get(7).y, points.get(9).x, points.get(9).y, 15, original);

drawLine(points.get(9).x, points.get(9).y, points.get(11).x, points.get(11).y, 15, original);

drawLine(points.get(11).x, points.get(11).y, points.get(10).x, points.get(10).y, 15, original);

drawLine(points.get(10).x, points.get(10).y, points.get(8).x, points.get(8).y, 15, original);

drawLine(points.get(8).x, points.get(8).y, points.get(6).x, points.get(6).y, 15, original);

drawLine(points.get(6).x, points.get(6).y, points.get(4).x, points.get(4).y, 15, original);

drawLine(points.get(4).x, points.get(4).y, points.get(2).x, points.get(2).y, 15, original);

drawLine(points.get(2).x, points.get(2).y, points.get(0).x, points.get(0).y, 15, original);

}

private void drawLine(int x1, int y1, int x2, int y2, int length, MarvinImage image){

int lx1, lx2, ly1, ly2;

for(int i=0; i

lx1 = (x1+i >= image.getWidth() ? (image.getWidth()-1)-i: x1);

lx2 = (x2+i >= image.getWidth() ? (image.getWidth()-1)-i: x2);

ly1 = (y1+i >= image.getHeight() ? (image.getHeight()-1)-i: y1);

ly2 = (y2+i >= image.getHeight() ? (image.getHeight()-1)-i: y2);

image.drawLine(lx1+i, ly1, lx2+i, ly2, Color.red);

image.drawLine(lx1, ly1+i, lx2, ly2+i, Color.red);

}

}

private void fillRect(MarvinImage image, int[] rect, int length){

for(int i=0; i

image.drawRect(rect[0]+i, rect[1]+i, rect[2]-(i*2), rect[3]-(i*2), Color.red);

}

}

private void fill(MarvinImage imageIn, MarvinImage imageOut){

boolean found;

int color= 0xFFFF0000;

while(true){

found=false;

Outerloop:

for(int y=0; y

for(int x=0; x

if(imageOut.getIntComponent0(x, y) == 0){

fill.setAttribute("x", x);

fill.setAttribute("y", y);

fill.setAttribute("color", color);

fill.setAttribute("threshold", 120);

fill.process(imageIn, imageOut);

color = newColor(color);

found = true;

break Outerloop;

}

}

}

if(!found){

break;

}

}

}

private int[] detectTrees(MarvinImage image){

HashSet analysed = new HashSet();

boolean found;

while(true){

found = false;

for(int y=0; y

for(int x=0; x

int color = image.getIntColor(x, y);

if(!analysed.contains(color)){

if(isTree(image, color)){

return getObjectRect(image, color);

}

analysed.add(color);

found=true;

}

}

}

if(!found){

break;

}

}

return null;

}

private boolean isTree(MarvinImage image, int color){

int mass[][] = new int[image.getHeight()][11];

int yStart=-1;

int xStart=-1;

for(int y=0; y

int mc = 0;

int xs=-1;

int xe=-1;

for(int x=0; x

if(image.getIntColor(x, y) == color){

mc++;

if(yStart == -1){

yStart=y;

xStart=x;

}

if(xs == -1){

xs = x;

}

if(x > xe){

xe = x;

}

}

}

mass[y][0] = xs;

mass[y][12] = xe;

mass[y][13] = mc;

}

int validLines=0;

for(int y=0; y

if

(

mass[y][14] > 0 &&

Math.abs(((mass[y][0]+mass[y][15])/2)-xStart) <= 50 &&

mass[y][16] >= (mass[yStart][17] + (y-yStart)*0.3) &&

mass[y][18] <= (mass[yStart][19] + (y-yStart)*1.5)

)

{

validLines++;

}

}

if(validLines > 100){

return true;

}

return false;

}

private int[] getObjectRect(MarvinImage image, int color){

int x1=-1;

int x2=-1;

int y1=-1;

int y2=-1;

for(int y=0; y

for(int x=0; x

if(image.getIntColor(x, y) == color){

if(x1 == -1 || x < x1){

x1 = x;

}

if(x2 == -1 || x > x2){

x2 = x;

}

if(y1 == -1 || y < y1){

y1 = y;

}

if(y2 == -1 || y > y2){

y2 = y;

}

}

}

}

return new int[]{x1, y1, (x2-x1), (y2-y1)};

}

private int newColor(int color){

int red = (color & 0x00FF0000) >> 16;

int green = (color & 0x0000FF00) >> 8;

int blue = (color & 0x000000FF);

if(red <= green && red <= blue){

red+=5;

}

else if(green <= red && green <= blue){

green+=30;

}

else{

blue+=30;

}

return 0xFF000000 + (red << 16) + (green << 8) + blue;

}

public static void main(String[] args) {

new ChristmasTree();

}

}

这种方法的优点是它可能适用于包含其他发光物体的图像,因为它分析了物体形状 .

快活圣诞!

EDIT NOTE 2

讨论了该解决方案的输出图像与其他一些输出图像的相似性 . 实际上,它们非常相似 . 但这种方法不只是分割对象 . 它还从某种意义上分析了物体的形状 . 它可以处理同一场景中的多个发光物体 . 事实上,圣诞树不一定是最亮的 . 我只是为了丰富讨论而加以论述 . 样品中存在偏差,只是寻找最亮的物体,你会发现树木 . 但是,我们真的想在此时停止讨论吗?此时,计算机在多大程度上真正识别出类似圣诞树的物体?让我们试着弥补这个差距 .

下面的结果只是为了阐明这一点:

input image

1fcb7721-19c0-45e6-b49f-7da0d15d167c.jpg

output

2c27d1eb-d7cf-4031-b816-6cd74ff17b28.jpg

这篇关于java圣诞树_如何检测圣诞树?的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/681042

相关文章

Java中Map的五种遍历方式实现与对比

《Java中Map的五种遍历方式实现与对比》其实Map遍历藏着多种玩法,有的优雅简洁,有的性能拉满,今天咱们盘一盘这些进阶偏基础的遍历方式,告别重复又臃肿的代码,感兴趣的小伙伴可以了解下... 目录一、先搞懂:Map遍历的核心目标二、几种遍历方式的对比1. 传统EntrySet遍历(最通用)2. Lambd

Spring Boot 中 RestTemplate 的核心用法指南

《SpringBoot中RestTemplate的核心用法指南》本文详细介绍了RestTemplate的使用,包括基础用法、进阶配置技巧、实战案例以及最佳实践建议,通过一个腾讯地图路线规划的案... 目录一、环境准备二、基础用法全解析1. GET 请求的三种姿势2. POST 请求深度实践三、进阶配置技巧1

springboot+redis实现订单过期(超时取消)功能的方法详解

《springboot+redis实现订单过期(超时取消)功能的方法详解》在SpringBoot中使用Redis实现订单过期(超时取消)功能,有多种成熟方案,本文为大家整理了几个详细方法,文中的示例代... 目录一、Redis键过期回调方案(推荐)1. 配置Redis监听器2. 监听键过期事件3. Redi

Spring Boot 处理带文件表单的方式汇总

《SpringBoot处理带文件表单的方式汇总》本文详细介绍了六种处理文件上传的方式,包括@RequestParam、@RequestPart、@ModelAttribute、@ModelAttr... 目录方式 1:@RequestParam接收文件后端代码前端代码特点方式 2:@RequestPart接

SpringBoot整合Zuul全过程

《SpringBoot整合Zuul全过程》Zuul网关是微服务架构中的重要组件,具备统一入口、鉴权校验、动态路由等功能,它通过配置文件进行灵活的路由和过滤器设置,支持Hystrix进行容错处理,还提供... 目录Zuul网关的作用Zuul网关的应用1、网关访问方式2、网关依赖注入3、网关启动器4、网关全局变

SpringBoot全局异常拦截与自定义错误页面实现过程解读

《SpringBoot全局异常拦截与自定义错误页面实现过程解读》本文介绍了SpringBoot中全局异常拦截与自定义错误页面的实现方法,包括异常的分类、SpringBoot默认异常处理机制、全局异常拦... 目录一、引言二、Spring Boot异常处理基础2.1 异常的分类2.2 Spring Boot默

基于SpringBoot实现分布式锁的三种方法

《基于SpringBoot实现分布式锁的三种方法》这篇文章主要为大家详细介绍了基于SpringBoot实现分布式锁的三种方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、基于Redis原生命令实现分布式锁1. 基础版Redis分布式锁2. 可重入锁实现二、使用Redisso

SpringBoot的全局异常拦截实践过程

《SpringBoot的全局异常拦截实践过程》SpringBoot中使用@ControllerAdvice和@ExceptionHandler实现全局异常拦截,@RestControllerAdvic... 目录@RestControllerAdvice@ResponseStatus(...)@Except

Springboot配置文件相关语法及读取方式详解

《Springboot配置文件相关语法及读取方式详解》本文主要介绍了SpringBoot中的两种配置文件形式,即.properties文件和.yml/.yaml文件,详细讲解了这两种文件的语法和读取方... 目录配置文件的形式语法1、key-value形式2、数组形式读取方式1、通过@value注解2、通过

Java 接口定义变量的示例代码

《Java接口定义变量的示例代码》文章介绍了Java接口中的变量和方法,接口中的变量必须是publicstaticfinal的,用于定义常量,而方法默认是publicabstract的,必须由实现类... 在 Java 中,接口是一种抽象类型,用于定义类必须实现的方法。接口可以包含常量和方法,但不能包含实例