基础算法篇-获取图片中连续像素集合

pojo:


public class ColorPo {
	@Override
	public String toString() {
		return "ColorPo [red=" + red + ", green=" + green + ", blue=" + blue + ", x=" + x + ", y=" + y + "]";
	}
	private int red;
	private int green;
	private int blue;
	private int x;
	private int y;
	
	ColorPo(){}
	ColorPo(int color,int x,int y){
		this.red = (color >> 16) & 0xFF;
		this.green = (color >> 8) & 0xFF;
		this.blue = color & 0xFF;
		this.x = x;
		this.y = y;
	}

	public boolean isBlack() {
		return sum() <= 300 ? true : false;
	}
	
	public int getX() {
		return x;
	}
	public void setX(int x) {
		this.x = x;
	}
	public int getY() {
		return y;
	}
	public void setY(int y) {
		this.y = y;
	}
	public int getColor() {
		int color = ((red*256)+green)*256+blue;
		return color > 8388608 ? color - 16777216 : color;
	}

	public int getRed() {
		return red;
	}

	public void setRed(int red) {
		this.red = red;
	}

	public int getGreen() {
		return green;
	}

	public void setGreen(int green) {
		this.green = green;
	}

	public int getBlue() {
		return blue;
	}

	public void setBlue(int blue) {
		this.blue = blue;
	}
	public int sum() {
		return red + blue + green;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + x;
		result = prime * result + y;
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ColorPo other = (ColorPo) obj;
		if (x != other.x)
			return false;
		if (y != other.y)
			return false;
		return true;
	}
	
	
}

util:


import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.imageio.ImageIO;


public class ImageUtils {
	//用于存储所有相邻的像素点
	private static List<Set<ColorPo>> colorList = new ArrayList<Set<ColorPo>>();
	public static void main(String[] args) throws Exception {
		File imageFile = new File("");
		BufferedImage read = ImageIO.read(imageFile);
		readImage(read);
		
	}
	
	/**
	 * 加载图片  遍历像素点
	 * @param image
	 */
	public static void readImage(BufferedImage image){
		Set<ColorPo> colorPoSet = new HashSet<ColorPo>();
		int z = 0;
		for (int i = 0; i < image.getHeight(); i++) {
			for (int j = 0; j < image.getWidth(); j++) {
				z++;
				ColorPo colorPo = new ColorPo(image.getRGB(i, j), i, j);
				if (colorPo.isBlack() && !colorPoSet.contains(colorPo)) {
					Set<ColorPo> colors = new HashSet<ColorPo>();
					System.out.println(1);
					colors.add(colorPo);
					getBlackArrays(image,i,j,colors);
					
					colorPoSet.addAll(colors);
					colorList.add(colors);
					colors = new HashSet<ColorPo>();
				} 
			}
		}
		System.out.println(colorPoSet.size());
		System.out.println(colorList.size());
		for (Set<ColorPo> set : colorList) {
			System.out.println(set.size());
		}
	}
	/**
	 * 获取相邻像素集合
	 * @param image
	 * @param x
	 * @param y
	 * @param colorPoSet 
	 * @param colorPoSet 
	 */
	public static void getBlackArrays(BufferedImage image,int x,int y, Set<ColorPo> colorPoSet) {
		int startX = 0,startY = 0,sum = 0;
		for (int i = 0; i < 8; i++) {
			switch (i) {
			//左上
			case 0:
				startX = x - 1;
				startY = y - 1;
				break;
			//上
			case 1:
				startX = x;
				startY = y - 1;
				break;
			//右上
			case 2:
				startX = x + 1;
				startY = y - 1;
				break;
			//左
			case 3:
				startX = x - 1;
				startY = y;
				break;
			//右
			case 4:
				startX = x + 1;
				startY = y;
				break;
			//左下
			case 5:
				startX = x - 1;
				startY = y + 1;
				break;
			//下
			case 6:
				startX = x;
				startY = y + 1;
				break;
			//右下
			case 7:
				startX = x + 1;
				startY = y + 1;
				break;
			}
			
			if (startX >=0 && startX < image.getWidth() && startY>= 0 && startY < image.getHeight()) {
				ColorPo colorPo = new ColorPo(image.getRGB(startX, startY), startX, startY);
				if (colorPo.isBlack() && !colorPoSet.contains(colorPo)) {
					colorPoSet.add(colorPo);
					sum++;
					getBlackArrays(image, startX, startY,colorPoSet);
				}
			}
		}
	}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值