服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Java获取彩色图像中的主色彩的实例代码

Java获取彩色图像中的主色彩的实例代码

2021-04-29 11:11yanan Java教程

这篇文章主要介绍了Java获取彩色图像中的主色彩的实例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

本文讲述了java获取彩色图像中的主色彩的实例代码。分享给大家供大家参考,具体如下:

一:基本思路

对于一张rgb色彩空间的彩色图像,很多时间我们想通过程序获得该图像有几种主要的色彩,但是对一般图像来说,在色彩交界处都是通过像素混合来实现自然过渡,所以直接扫描图像的像素值,得到的不同颜色值可能多达上百中,而实际上图像可能只有3~4种的主要色彩,如何去掉那些混合颜色,准确提取出来这3~4中的主色彩,根据一般图像的特征,图像在不同色彩的边界处混合不同的颜色值,此可以视为图像的边缘特性之一,因此可以根据简单的边缘梯度算法实现这些混合像素的提取得到输出的像素值数组,然后扫描每个像素值,寻找指定半径参数r周围的像素,发现为零,而且距离中心像素最近的像素点的值做为中心像素的像素值,扫描结束以后输出像素数组,然后对数组线性扫描,即可得到图片的主要色彩rgb值。

二:实现步骤
1.      输入图像数组,对彩色图像灰度化;
2.      对灰度化以后的图像,计算图像梯度,这里使用sobol算子;
3.      对得到每一个非零像素点实现半径为r的范围内的扫描,找出与之最相近的为零的原像素值;
4.      对得到数组进行简单的扫描,得到主色彩。

其中参数r是要根据不同应用场景,找到最合适的值。理论上图像越大,r的取值也应该越大,否则算法会失准。

三:原图及运行效果

Java获取彩色图像中的主色彩的实例代码

原图

Java获取彩色图像中的主色彩的实例代码

算法运行之后提取到四种主要色彩

四:算法实现源代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
public static bufferedimage removeblendpixels(bufferedimage image, int raidus) {
  int width = image.getwidth();
  int height = image.getheight();
  int[] pixels = new int[width * height];
  getrgb(image, 0, 0, width, height, pixels);
  // 创建处理结果
  bufferedimage resultimg = createcompatibledestimage(image, null);
  setrgb(resultimg, 0, 0, width, height, pixels);
  // 灰度化与梯度求取
  byte[] graydata = getgraydata(pixels, width, height);
  byte[] binarydata = getgrident(graydata, width, height);
  int index = 0;
  for (int row = 1; row < height - 1; row++) {
   for (int col = 1; col < width - 1; col++) {
    index = row * width + col;
    int pixel = (binarydata[index] & 0xff);
    if (pixel > 0) {
     // 半径扫描操作
     int mindis = integer.max_value;
     int minrow = -1;
     int mincol = -1;
     int nr = 0;
     int nc = 0;
     int index2 = 0;
     for (int subrow = -raidus; subrow <= raidus; subrow++) {
      nr = row + subrow;
      if (nr < 0 || nr >= height) {
       continue;
      }
      for (int subcol = -raidus; subcol <= raidus; subcol++) {
       nc = col + subcol;
       if (nc < 0 || nc >= width) {
        continue;
       }
       index2 = nr * width + nc;
       int value = (binarydata[index2] & 0xff);
       if (value == 0) {
        int distance = distancecolor(image.getrgb(nc, nr), image.getrgb(col, row));
        if (distance < mindis) {
         mindis = distance;
         minrow = nr;
         mincol = nc;
        }
       }
      }
     }
     resultimg.setrgb(col, row, image.getrgb(mincol, minrow));
    }
   }
  }
  return resultimg;
 }
 public static int distancecolor(int rgb, int rgb2) {
  // color one
  int r1 = (rgb >> 16) & 0xff;
  int g1 = (rgb >> 8) & 0xff;
  int b1 = rgb & 0xff;
  // color two
  int r2 = (rgb2 >> 16) & 0xff;
  int g2 = (rgb2 >> 8) & 0xff;
  int b2 = rgb2 & 0xff;
  // distance
  int rr = r1 - r2;
  int gg = g1 - g2;
  int bb = b1 - b2;
  int sum = (int) math.sqrt(rr * rr + gg * gg + bb * bb);
  return sum;
 }
 public static byte[] getgraydata(int[] inpixels, int width, int height) {
  // 图像灰度化
  byte[] outpixels = new byte[width * height];
  int index = 0;
  for (int row = 0; row < height; row++) {
   int tr = 0, tg = 0, tb = 0;
   for (int col = 0; col < width; col++) {
    index = row * width + col;
    tr = (inpixels[index] >> 16) & 0xff;
    tg = (inpixels[index] >> 8) & 0xff;
    tb = inpixels[index] & 0xff;
    int gray = (int) (0.299 * tr + 0.587 * tg + 0.114 * tb);
    outpixels[index] = (byte) (gray & 0xff);
   }
  }
  return outpixels;
 }
 public static byte[] getgrident(byte[] inpixels, int width, int height) {
  byte[] outpixels = new byte[width * height];
  int index = 0;
  for (int row = 0; row < height; row++) {
   int tr = 0;
   for (int col = 0; col < width; col++) {
    if (row == 0 || col == 0 || (row == height - 1) || (col == width - 1)) {
     index = row * width + col;
     outpixels[index] = (byte) (0x00);
     continue;
    }
    int xg = 0, yg = 0;
    for (int sr = -1; sr <= 1; sr++) {
     for (int sc = -1; sc <= 1; sc++) {
      int nrow = row + sr;
      int ncol = col + sc;
      if (nrow < 0 || nrow >= height) {
       nrow = 0;
      }
      if (ncol < 0 || ncol >= width) {
       ncol = 0;
      }
      index = nrow * width + ncol;
      tr = (inpixels[index] & 0xff);
      xg += x_sobel[sr + 1][sc + 1] * tr;
      yg += y_sobel[sr + 1][sc + 1] * tr;
     }
    }
    index = row * width + col;
    int g = (int) math.sqrt(xg * xg + yg * yg);
    outpixels[index] = (byte) (clamp(g) & 0xff);
   }
  }
  return outpixels;
 }

需要定义的常量值如下:

?
1
2
3
public static final int[][] x_sobel = new int[][] { { -1, -2, -1 }, { 0, 0, 0 }, { 1, 2, 1 } };
public static final int[][] y_sobel = new int[][] { { -1, 0, 1 }, { -2, 0, 2 }, { -1, 0, 1 } };
public static final int block_pixel_radius = 5;

梯度求取使用是sobol算子,对处理以后的bufferedimage对象扫描获取主色彩的代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int width = result.getwidth();
int height = result.getheight();
map<integer, integer> colorindexmap = new hashmap<integer, integer>();
for (int row = 0; row < height; row++) {
 for (int col = 0; col < width; col++) {
  int pixelvalue = result.getrgb(col, row);
  if (!colorindexmap.containskey(pixelvalue)) {
   colorindexmap.put(pixelvalue, pixelvalue);
  }
 }
}
// now scan pixel value
// return result
system.out.println("number of color = " + colorindexmap.size());
return colorindexmap.keyset().toarray(new integer[0]);

测试代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
public static void main(string[] args) {
 file file = new file("d:\\gloomyfish\\bigmonkey.png");
 file resultfile = new file("d:\\gloomyfish\\result.png");
 try {
  bufferedimage image = imageio.read(file);
  bufferedimage result = removeblendpixels(image, block_pixel_radius);
  imageio.write(result, "png", resultfile);
  integer[] colors = extractcolors(result);
  system.out.println("total colors : " + colors.length);
 } catch (ioexception e) {
  e.printstacktrace();
 }
}

 注意:主要的关键在于对待处理图像输入正确大小的半径,这个半径大小跟图像实际大小相关,而且算法可以近一步优化成不依赖半径参数的版本,知道找到等于零的像素为止。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/jia20003/article/details/50414595

延伸 · 阅读

精彩推荐