📄 home.java
字号:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
class View_Panel extends Panel
{
Image img, R_img, G_img, B_img, R_mask_img, G_mask_img, B_mask_img, Gray_mask_img;
byte head1 [], head2 [], bgr [];
int R_value [], G_value [], B_value [];
int R_gray [], G_gray [], B_gray [], pix [];
int R_mask [], G_mask [], B_mask [], Gray_mask [];
int r [][], g [][], b [][], gray [][];
int R_buf, G_buf, B_buf;
int width, height, bitcnt, imgsize, pad, idx, i, j, x, y;
int SMask_X [][] = {{1, 0, -1}, {2, 0, -2}, {1, 0, -1}};
int SMask_Y [][] = {{-1, -2, -1}, {0, 0, 0}, {1, 2, 1}};
public void Image_Load ()
{
head1 = new byte [14];
head2 = new byte [40];
try
{
FileInputStream fis = new FileInputStream ("E:/imageprocess/Mandrill.bmp");
fis.read (head1, 0, 14);
fis.read (head2, 0, 40);
width = (int) ((head2 [4] & 0xff) | ((head2 [5] & 0xff) << 8) |
((head2 [6] & 0xff) << 16) | ((head2 [7] & 0xff) << 24));
height = (int) ((head2 [8] & 0xff) | ((head2 [9] & 0xff) << 8) |
((head2 [10] & 0xff) << 16) | ((head2 [11] & 0xff) << 24));
bitcnt = (int) ((head2 [14] & 0xff) | ((head2 [15] & 0xff) << 8));
imgsize = (int) ((head2 [20] & 0xff) | ((head2 [21] & 0xff) << 8) |
((head2 [22] & 0xff) << 16) | ((head2 [23] & 0xff) << 24));
if (bitcnt != 24)
{
System.out.println ("not 24 bit map...");
System.exit (0);
}
pad = (imgsize / height) - width * 3;
R_value = new int [width * height];
G_value = new int [width * height];
B_value = new int [width * height];
R_mask = new int [width * height];
G_mask = new int [width * height];
B_mask = new int [width * height];
Gray_mask = new int [width * height];
R_gray = new int [width * height];
G_gray = new int [width * height];
B_gray = new int [width * height];
pix = new int [width * height];
r = new int [width][height];
g = new int [width][height];
b = new int [width][height];
gray = new int [width][height];
bgr = new byte [imgsize];
fis.read (bgr, 0, imgsize);
for (i = 0; i < height; i++)
{
for (j = 0; j < width; j++)
{
B_value [width * (height - i - 1) + j] = (int) (bgr [idx] & 0xff);
G_value [width * (height - i - 1) + j] = (int) (bgr [idx + 1] & 0xff);
R_value [width * (height - i - 1) + j] = (int) (bgr [idx + 2] & 0xff);
R_buf = R_value [width * (height - i - 1) + j];
G_buf = G_value [width * (height - i - 1) + j];
B_buf = B_value [width * (height - i - 1) + j];
R_gray [width * (height - i - 1) + j] = 255 << 24 | R_buf << 16 |
R_buf << 8 | R_buf;
G_gray [width * (height - i - 1) + j] = 255 << 24 | G_buf << 16 |
G_buf << 8 | G_buf;
B_gray [width * (height - i - 1) + j] = 255 << 24 | B_buf << 16 |
B_buf << 8 | B_buf;
pix [width * (height - i - 1) + j] = 255 << 24 | R_buf << 16 |
G_buf << 8 | B_buf;
r [i][j] = R_buf;
g [i][j] = G_buf;
b [i][j] = B_buf;
gray [i][j] = (int) (Math.sqrt ((double) ((R_buf * R_buf) +
(G_buf * G_buf) + (B_buf * B_buf))));
idx += 3;
}
idx += pad;
}
int Rm, Gm, Bm, Graym;
for (i = 0; i < height - 2; i++)
{
for (j = 0; j < width - 2; j++)
{
int X_value = 0;
int Y_value = 0;
int Sum = 0;
int X_value1 = 0;
int Y_value1 = 0;
int Sum1 = 0;
int X_value2 = 0;
int Y_value2 = 0;
int Sum2 = 0;
int X_value3 = 0;
int Y_value3 = 0;
int Sum3 = 0;
for (x = 0; x < 3; x++)
{
for (y = 0; y < 3; y++)
{
Rm = r [i + x][j + y];
X_value += Rm * SMask_X [x][y];
Y_value += Rm * SMask_Y [x][y];
Gm = g [i + x][j + y];
X_value1 += Gm * SMask_X [x][y];
Y_value1 += Gm * SMask_Y [x][y];
Bm = b [i + x][j + y];
X_value2 += Bm * SMask_X [x][y];
Y_value2 += Bm * SMask_Y [x][y];
Graym = gray [i + x][j + y];
X_value3 += Graym * SMask_X [x][y];
Y_value3 += Graym * SMask_Y [x][y];
}
}
Sum = (int) Math.abs (X_value) + (int) Math.abs (Y_value);
Sum1 = (int) Math.abs (X_value1) + (int) Math.abs (Y_value2);
Sum2 = (int) Math.abs (X_value2) + (int) Math.abs (Y_value2);
Sum3 = (int) Math.abs (X_value3) + (int) Math.abs (Y_value3);
if (Sum > 255)
{
Sum = 255;
}
else if (Sum < 0)
{
Sum = 0;
}
if (Sum1 > 255)
{
Sum1 = 255;
}
else if (Sum1 < 0)
{
Sum1 = 0;
}
if (Sum2 > 255)
{
Sum2 = 255;
}
else if (Sum2 < 0)
{
Sum2 = 0;
}
if (Sum3 > 255)
{
Sum3 = 255;
}
else if (Sum3 < 0)
{
Sum3 = 0;
}
R_mask [width * (height - i - 1) + j] = 255 << 24 | Sum << 16 |
Sum << 8 | Sum;
G_mask [width * (height - i - 1) + j] = 255 << 24 | Sum1 << 16 |
Sum1 << 8 | Sum1;
B_mask [width * (height - i - 1) + j] = 255 << 24 | Sum2 << 16 |
Sum2 << 8 | Sum2;
Gray_mask [width * (height - i - 1) + j] = 255 << 24 | Sum3 << 16 |
Sum3 << 8 | Sum3;
}
}
img = createImage (new MemoryImageSource (width, height, pix, 0, width));
R_img = createImage (new MemoryImageSource (width, height,R_gray, 0, width));
G_img = createImage (new MemoryImageSource (width, height, G_gray, 0, width));
B_img = createImage (new MemoryImageSource (width, height, B_gray, 0, width));
R_mask_img = createImage (new MemoryImageSource (width, height,R_mask, 0, width));
G_mask_img = createImage (new MemoryImageSource (width, height, G_mask, 0, width));
B_mask_img = createImage (new MemoryImageSource (width, height, B_mask, 0, width));
Gray_mask_img = createImage (new MemoryImageSource (width, height, Gray_mask,
0, width));
fis.close ();
}
catch (Exception e)
{
System.out.println (e);
}
}
public void paint (Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
g.drawImage (img, 0, 0, this);
g2.drawString ("Origin", 100, 280);
g.drawImage (R_img, 256, 0, this);
g2.drawString ("Red Chanel", 380, 280);
g.drawImage (G_img, 512, 0, this);
g2.drawString ("Green Chanel", 630, 280);
g.drawImage (B_img, 768, 0, this);
g2.drawString ("Blue chanel", 880, 280);
g.drawImage (Gray_mask_img, 0, 400, this);
g2.drawString ("Gray Scale edge map", 100, 680);
g.drawImage (R_mask_img, 256, 400, this);
g2.drawString ("Red chanel edges", 380, 680);
g.drawImage (G_mask_img, 512, 400, this);
g2.drawString ("Green chanel edges", 630, 680);
g.drawImage (B_mask_img, 768, 400, this);
g2.drawString ("Blue chanel edges", 880, 680);
}
public void Func ()
{
Image_Load ();
repaint ();
}
}
public class home extends Frame
{
View_Panel vp ;
public home ()
{
super ("Chanel Separate & Sobel Mask View");
addWindowListener (new MyAdapter ());
vp = new View_Panel ();
add (vp);
vp.Func ();
setSize (1024, 768);
show ();
}
public static void main (String [] args)
{
new home ();
}
}
class MyAdapter extends WindowAdapter
{
public void windowClosing (WindowEvent e)
{
System.exit (0);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -