• 検索結果がありません。

( ) p.1 x y y = ( x ) 1 γ γ = filtergamma.java import java.applet.*; public class filtergamma extends Applet{ Image img; Image new_img; publi

N/A
N/A
Protected

Academic year: 2021

シェア "( ) p.1 x y y = ( x ) 1 γ γ = filtergamma.java import java.applet.*; public class filtergamma extends Applet{ Image img; Image new_img; publi"

Copied!
10
0
0

読み込み中.... (全文を見る)

全文

(1)

音声画像処理・自習課題

e055701D

 新垣祐一郎

提出日:2007 年  12 月 13 日(木曜)

(

)

、導出を行いなさい。

・四角い模様を生成するプログラムを作成しなさい。

Figure 1: 四角を描く

ソースプログラム  shikaku.java

import java.applet.*; 1 import java.awt.*; 2 import java.awt.image.*; 3 4

public class shikaku extends Applet{

5

public void paint( Graphics g)

6

{

7

g.drawRect( 10,10,100,100 ); // x(10,10) width = 100,height=100

8 } 9 } 10 10行目のg.drawRect( 10,10,100,100 );によって、座標(10, 10)を起点に、横幅100、 縦幅100の四角形を描いている。

(2)

(

)

画像の変換

テキスト p.91 の列を参考にして、ガンマ変換を行うプログラムを

作りなさい。

ガンマ変換とは、画像の濃淡値xを 以下の式によってyに変換するものである。

y = 255(

x

255

)

1 γ

   左:元画像        右:ガンマ変換後 ( γ = 0.05)

メインのプログラム  filterGamma.java

import java.applet.*; 1 import java.awt.*; 2 import java.awt.image.*; 3 4

public class filterGamma extends Applet{

5 Image img; 6 Image new_img; 7 8

public void init(){

9

img = getImage(getCodeBase(),"dl.jpg");

10

filter_shori();

(3)

}

12 13

void filter_shori(){

14

ImageFilter f = new GammaFilter(); // <-- Filter Program

15

FilteredImageSource fi = new FilteredImageSource(img.getSource(),f);

16 new_img = createImage(fi); 17 } 18 19

public void paint(Graphics g){

20

MediaTracker mt = new MediaTracker(this);

21 mt.addImage(new_img,0); 22 try{ 23 mt.waitForID(0); 24 } 25 catch(InterruptedException e){} 26

g.drawImage(img,0,0,this); // Original Image

27

int w = img.getWidth(this) + 20;

28

g.drawImage(new_img,w,0,this); // new Image

29 } 30 } 31 32 上記の15行目で 以下のプログラムで定義した関数GammaFilter()を呼び出す。

フィルターのプログラム  GammaFilter.java

import java.awt.*; 1 import java.awt.image.*; 2 3

class GammaFilter extends RGBImageFilter{

4 public GammaFilter(){ 5 canFilterIndexColorModel = true; 6 } 7

public int filterRGB(int x, int y, int rgb){

8

int rr, gg, bb;

9

int mean;

10

double gamma = 0.05; // <-- GAMMA value

11 12 rr = (rgb >> 16) & 0xff; 13 gg = (rgb >> 8) & 0xff; 14 bb = (rgb >> 0) & 0xff; 15

mean = (rr + gg + bb)/3; // GrayScale CONVERTing

16

mean = 255*(mean/255)^(int)(1/gamma); // <-- GAMMA henkan

17 18 return((rgb & 0xffffffff) | 19 (mean << 16) | 20 (mean << 8) | 21 (mean << 0)); 22 } 23 } 24 上記のプログラムの 17行目 でガンマ変換の計算を実行している。

(4)

(

)

空間フィルタ

・テキスト p.108 の平均化フィルタを行うプログラムを作成し、実行しなさい。 また、平均化フィルタを反復適用すると、どういう変化が起きるのか、わかりや すく示しなさい。

1、平均化フィルタ

平均化フィルタはlow-passフィルタで、像から高周波成分を除去するものである。しかし 像中の鋭いエッジも除いてしまうため像をぼけさせる。反復して3回フィルタリングして みると、以下のように変化していった。 元の画像 平均化1回目 平均化2回目 平均化3回目

(5)

平均化フィルタのプログラム  filterHeikin.java

import java.awt.*; 1 import java.awt.image.*; 2 import java.io.*; 3

import javax.imageio.*; // for File INPUTandOUTPUT

4 5

public class filterHeikin {

6

public static void main(String[] args) throws Exception {

7

BufferedImage src = ImageIO.read(new File("maggotbrain1.jpg")); // Image READing

8 String format = "jpg"; 9 test(src, format); 10 } 11

private static void test(BufferedImage src, String format) throws Exception {

12 13

src = normalize(src);

14

Kernel[] k = new Kernel[]{

15 imgAverageFiltered, 16 }; 17 18

String[] n = new String[]{

19 "imgAverageFiltered", 20 }; 21 22

for(int i=0; i<k.length; i++){

23 ImageIO.write( 24 getFilteredImage(src, k[i]), 25 format, 26

new File(n[i] + "." + format));

27 } 28 } 29 30

private static BufferedImage normalize(BufferedImage src){

31

BufferedImage dst =

32

new BufferedImage(

33

src.getWidth(), src.getHeight(), BufferedImage.TYPE_INT_RGB);

34 Graphics2D g = dst.createGraphics(); 35 g.drawImage(src, 0, 0, null); 36 g.dispose(); 37 return dst; 38 } 39 40

private static BufferedImage getFilteredImage(BufferedImage src, Kernel filterMatrix) {

41

BufferedImage dst =

42

new BufferedImage(

43

src.getWidth(), src.getHeight(), BufferedImage.TYPE_INT_RGB);

44

ConvolveOp op = new ConvolveOp(filterMatrix);

45 op.filter(src, dst); 46 return dst; 47 } 48 49 /* NAMERAKA 3 x 3 Filter*/ 50

private static final Kernel imgAverageFiltered =

51

new Kernel(

(6)

3, 3, 53 new float[] { 54 1/9f, 1/9f, 1/9f, 55 1/9f, 1/9f, 1/9f, 56 1/9f, 1/9f, 1/9f, 57 } 58 ); 59 } 60 上記の55∼57行目までに記した行列式が平均化を表す。ピクセルごとのそれぞれ の値にかける重みを 19 の強さにすることでぼかしの効果が生まれる。

2、ラプラシアンフィルタ

・テキスト p.119 のラプラシアンフィルタを行うプログラムを作成

し、濃淡画像に施してみなさい。

元の画像 ラプラシアンフィルタをかけた画像

ラプラシアンフィルタのプログラム  filterLaplacian.java

import java.awt.*; 1 import java.awt.image.*; 2 import java.io.*; 3

import javax.imageio.*; // for File INPUTandOUTPUT

4 5

public class filterLaplacian {

6

public static void main(String[] args) throws Exception {

7

BufferedImage src = ImageIO.read(new File("Buddha.jpg")); // Image READing

8

String format = "jpg";

9

test(src, format);

(7)

}

11

private static void test(BufferedImage src, String format) throws Exception {

12 13

src = normalize(src);

14

Kernel[] k = new Kernel[]{

15 imgLaplacianFiltered, 16 }; 17 18

String[] n = new String[]{

19 "imgLaplacianFiltered", 20 }; 21 22

for(int i=0; i<k.length; i++){

23 ImageIO.write( 24 getFilteredImage(src, k[i]), 25 format, 26

new File(n[i] + "." + format));

27 } 28 } 29 30

private static BufferedImage normalize(BufferedImage src){

31

BufferedImage dst =

32

new BufferedImage(

33

src.getWidth(), src.getHeight(), BufferedImage.TYPE_INT_RGB);

34 Graphics2D g = dst.createGraphics(); 35 g.drawImage(src, 0, 0, null); 36 g.dispose(); 37 return dst; 38 } 39 40

private static BufferedImage getFilteredImage(BufferedImage src, Kernel filterMatrix) {

41

BufferedImage dst =

42

new BufferedImage(

43

src.getWidth(), src.getHeight(), BufferedImage.TYPE_INT_RGB);

44

ConvolveOp op = new ConvolveOp(filterMatrix);

45 op.filter(src, dst); 46 return dst; 47 } 48 49 /* Laplacian 3 x 3 Filter*/ 50

private static final Kernel imgLaplacianFiltered =

51 new Kernel( 52 3, 3, 53 new float[] { 54 1f, 1f, 1f, 55 1f, -8f, 1f, 56 1f, 1f, 1f, 57 } 58 ); 59 } 60 上記の55∼57行目までに記した行列式がラプラシアンフィルタを実現している。8 方向のラプラシアンフィルタは、ある注目画素を中心とした上下左右の9つの画素値に対 して、上記のような係数をそれぞれ乗算し、結果を合計する。この合計した値が新しい画 素値となる。

(8)

3、鮮鋭化フィルタ

・テキスト p.123 の鮮鋭化フィルタを行うプログラムを作成しな

さい。

元の画像

(9)

鮮鋭化フィルタのプログラム  filterSeneika.java

import java.applet.*; 1 import java.awt.*; 2 import java.awt.image.*; 3 import java.io.*; 4

import javax.imageio.*; // for File INPUTandOUTPUT

5 6

public class filterSeneika {

7

public static void main(String[] args) throws Exception {

8

BufferedImage src = ImageIO.read(new File("piraColor.jpg")); // Image READing

9 String format = "jpg"; 10 test(src, format); 11 } 12

private static void test(BufferedImage src, String format) throws Exception {

13 14

src = normalize(src);

15

Kernel[] k = new Kernel[]{

16 imgSeneikaFiltered, 17 }; 18 19

String[] n = new String[]{ // define the FileName for Output

20 "imgSeneikaFiltered", 21 }; 22 23

for(int i=0; i<k.length; i++){

24 ImageIO.write( 25 getFilteredImage(src, k[i]), // 26 format, 27

new File(n[i] + "." + format)); // FileName for Output

28 } 29 } 30 31

private static BufferedImage normalize(BufferedImage src){

32

BufferedImage dst =

33

new BufferedImage(

34

src.getWidth(), src.getHeight(), BufferedImage.TYPE_INT_RGB);

35 Graphics2D g = dst.createGraphics(); 36 g.drawImage(src, 0, 0, null); 37 g.dispose(); 38 return dst; 39 } 40 41

private static BufferedImage getFilteredImage(BufferedImage src, Kernel filterMatrix){

42

BufferedImage dst =

43

new BufferedImage(

44

src.getWidth(), src.getHeight(), BufferedImage.TYPE_INT_RGB);

45

ConvolveOp op = new ConvolveOp(filterMatrix);

46 op.filter(src, dst); 47 return dst; 48 } 49 50 /* SENEI Filter*/ 51

private static final Kernel imgSeneikaFiltered =

(10)

new Kernel( 53 3, 3, 54 new float[] { 55 0f, -1f, 0f, 56 -1f, 5f, -1f, 57 0f, -1f, 0f, 58 } 59 ); 60 } 61 鮮鋭化フィルタは、エッジ部分を強調するフィルタであり、以下の行列式で表すこと ができる。 [ 0 −k 0 −k 1 + 4k −k 0 −k 0 ] 入力画像の行列に上記の行列を引くと、鮮鋭化フィルタを実現できる。ソースプログラム の56∼58行目の行列式は、k =1のときを考えたものである。

参照

関連したドキュメント

In the second section, we study the continuity of the functions f p (for the definition of this function see the abstract) when (X, f ) is a dynamical system in which X is a

We study a Neumann boundary-value problem on the half line for a second order equation, in which the nonlinearity depends on the (unknown) Dirichlet boundary data of the solution..

Acknowledgement.This work was partially done while the second author was visiting the University of Texas at Austin and Texas A&amp;M University, and in the Linear Analysis Workshop

Algebraic curvature tensor satisfying the condition of type (1.2) If ∇J ̸= 0, the anti-K¨ ahler condition (1.2) does not hold.. Yet, for any almost anti-Hermitian manifold there

In this paper, for each real number k greater than or equal to 3 we will construct a family of k-sum-free subsets (0, 1], each of which is the union of finitely many intervals

Some of the known oscillation criteria are established by making use of a technique introduced by Kartsatos [5] where it is assumed that there exists a second derivative function

In Subsection 5.1 we show the continuity of the Dirichlet heat kernel associated with the killed LBM on a bounded open set by using its eigenfunction expansion, and in Subsection 5.2

Using the previous results as well as the general interpolation theorem to be given below, in this section we are able to obtain a solution of the problem, to give a full description