
1. 委派可以將方法當成參數來進行傳遞委派語法:
[public|private|protected] Delegate [void | 回傳資料型態] 委派名稱 ([參數1, 參數2,…]);


1: abstract class AbstractClass
2: {3: protected abstract void proc1();
4: protected abstract void proc2();
5: protected abstract void proc3();
6: public void TemplateMethod()
7: { 8: proc1(); 9: proc2(); 10: proc3();11: MessageBox.Show("TemplateMethod");
12: } 13: }



1: package Basic;
2: 3: import java.awt.EventQueue;
4: import java.awt.image.BufferedImage;
5: 6: import javax.swing.ImageIcon;
7: import javax.swing.JFrame;
8: import javax.swing.JOptionPane;
9: import javax.swing.JSlider;
10: import javax.swing.JLabel;
11: import javax.swing.event.ChangeListener;
12: import javax.swing.event.ChangeEvent;
13: 14: import org.opencv.core.Core;
15: import org.opencv.core.Mat;
16: import org.opencv.imgcodecs.Imgcodecs;
17: import org.opencv.imgproc.Imgproc;
18: 19: public class AdjContrastDemo {
20: static{System.loadLibrary(Core.NATIVE_LIBRARY_NAME);}
21: private JFrame frmContrastAdjustment;
22: static double alpha = 1;
23: static double beta = 50;
24: JSlider slider; 25: JSlider slider_1; 26: JLabel lblAlphaVal; 27: JLabel lblBetaVal; 28: JLabel lblImage;29: static Mat imgSrc;
30: static Mat imgG = new Mat();
31: static Mat imgDst = new Mat();
32: /**
33: * Launch the application.
34: */
35: public static void main(String[] args) {
36: EventQueue.invokeLater(new Runnable() {
37: public void run() {
38: try {
39: //imgSrc = Imgcodecs.imread("C:\\OpenCV\\images\\left01.jpg");
40: imgSrc = Imgcodecs.imread("C:\\OpenCV\\images\\lena.jpg");
41: if(imgSrc.empty())
42: {43: JOptionPane.showMessageDialog(null, "無法讀取圖片","警告",
44: JOptionPane.WARNING_MESSAGE); 45: }46: if(imgSrc.channels()==3)
47: imgSrc.copyTo(imgG);48: //Imgproc.cvtColor(imgSrc, imgG, Imgproc.COLOR_BGR2GRAY);
49: else
50: imgSrc.copyTo(imgG);51: //imgDst = new Mat();
52: AdjContrastDemo window = new AdjContrastDemo();
53: window.frmContrastAdjustment.setVisible(true);54: } catch (Exception e) {
55: e.printStackTrace(); 56: } 57: } 58: }); 59: } 60: 61: /**
62: * Create the application.
63: */
64: public AdjContrastDemo() {
65: initialize(); 66: } 67: 68: /**
69: * Initialize the contents of the frame.
70: */
71: private void initialize() {
72: frmContrastAdjustment = new JFrame();
73: frmContrastAdjustment.setTitle("Contrast Adjustment");
74: frmContrastAdjustment.setBounds(100, 100, 725, 692); 75: frmContrastAdjustment.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 76: frmContrastAdjustment.getContentPane().setLayout(null); 77: 78: slider = new JSlider();
79: slider.setValue((int)alpha);
80: slider.addChangeListener(new ChangeListener() {
81: public void stateChanged(ChangeEvent arg0) {
82: lblAlphaVal.setText(slider.getValue()+"");
83: alpha = slider.getValue(); 84: AdjustContrast(); 85: } 86: }); 87: 88: slider.setBounds(125, 40, 370, 19); 89: frmContrastAdjustment.getContentPane().add(slider); 90: 91: slider_1 = new JSlider();
92: slider_1.setValue((int)beta);
93: slider_1.addChangeListener(new ChangeListener() {
94: public void stateChanged(ChangeEvent arg0) {
95: lblBetaVal.setText(slider_1.getValue()+"");
96: beta = slider_1.getValue(); 97: AdjustContrast(); 98: } 99: }); 100: slider_1.setBounds(125, 90, 370, 19); 101: frmContrastAdjustment.getContentPane().add(slider_1); 102: 103: JLabel lblAlpha = new JLabel();
104: lblAlpha.setText("alpha");
105: lblAlpha.setBounds(55, 29, 71, 36); 106: frmContrastAdjustment.getContentPane().add(lblAlpha); 107: 108: JLabel lblBeta = new JLabel();
109: lblBeta.setText("beta");
110: lblBeta.setBounds(55, 80, 71, 36); 111: frmContrastAdjustment.getContentPane().add(lblBeta); 112: 113: lblAlphaVal = new JLabel("0");
114: lblAlphaVal.setBounds(499, 29, 55, 36);115: lblAlphaVal.setText(""+alpha);
116: frmContrastAdjustment.getContentPane().add(lblAlphaVal); 117: 118: lblBetaVal = new JLabel("0");
119: lblBetaVal.setBounds(499, 80, 55, 36);120: lblBetaVal.setText(""+beta);
121: frmContrastAdjustment.getContentPane().add(lblBetaVal); 122: 123: lblImage = new JLabel("");
124: lblImage.setBounds(31, 154, 587, 467); 125: frmContrastAdjustment.getContentPane().add(lblImage); 126: }127: public void AdjustContrast()
128: { 129: imgG.convertTo(imgDst, -1, alpha, beta); 130: BufferedImage bufImg = matToBufferedImage(imgDst);131: lblImage.setIcon(new ImageIcon(bufImg));
132: }133: public BufferedImage matToBufferedImage(Mat matrix){
134: int cols = matrix.cols();
135: int rows = matrix.rows();
136: int elemSize = (int)matrix.elemSize();
137: byte[] data = new byte[cols*rows*elemSize];
138: if(matrix.channels()==3)
139: Imgproc.cvtColor(matrix, matrix, Imgproc.COLOR_BGR2RGB); 140: matrix.get(0, 0, data); 141: BufferedImage bufImg = null;142: switch(matrix.channels())
143: {144: case 1:
145: bufImg = new BufferedImage(cols, rows, BufferedImage.TYPE_BYTE_GRAY);
146: break;
147: case 3:
148: bufImg = new BufferedImage(cols,rows, BufferedImage.TYPE_3BYTE_BGR);
149: break;
150: } 151: bufImg.getRaster().setDataElements(0, 0, cols, rows, data);152: return bufImg;
153: } 154: }

1: public BufferedImage matToBufferedImage(Mat matrix){
2: int cols = matrix.cols();
3: int rows = matrix.rows();
4: int elemSize = (int)matrix.elemSize();
5: byte[] data = new byte[cols*rows*elemSize];
6: matrix.get(0, 0, data); 7: BufferedImage bufImg = null;8: switch(matrix.channels())
9: {10: case 1:
11: bufImg = new BufferedImage(cols, rows, BufferedImage.TYPE_BYTE_GRAY);
12: break;
13: case 3:
14: bufImg = new BufferedImage(cols,rows, BufferedImage.TYPE_3BYTE_BGR);
15: break;
16: } 17: bufImg.getRaster().setDataElements(0, 0, cols, rows, data);18: return bufImg;
19: }


1: package ex1;
2: 3: public class ex1_helloworld {
4: 5: public static void main(String[] args) {
6: // TODO Auto-generated method stub
7: System.out.println("Hello World!");
8: } 9: 10: }


1: 2: for (int i = 0; i < contours.size(); i++) {
3: cout << "contour # " << i << endl;
4: imgMask = Mat::zeros(imgSrc.size(), CV_8UC1); 5: drawContours(imgMask, contours, i, Scalar(255), -1); 6: Mat imgROI; 7: imgSrc.copyTo(imgROI, imgMask); 8: string name = std::to_string(i);9: //imshow(name, imgROI);
10: cout << "mean color (BGR) = " << mean(imgSrc, imgMask) << endl;
11: Moments m = moments(contours[i]);12: cout << "area = " << m.m00 <<endl;
13: double contArea = contourArea(contours[i]);
14: cout << "contour area = " << contArea << endl;
15: double cx = m.m10 / m.m00;
16: double cy = m.m01 / m.m00;
17: line(imgROI, Point(cx, cy), Point(cx, cy), Scalar(255, 0, 0), 5, 8);18: char label[50];
19: sprintf_s(label, "(%.0f,%.0f)", cx, cy);
20: int offset = 100;
21: putText(imgROI, label, Point(cx-offset, cy), FONT_HERSHEY_COMPLEX, 1, Scalar(0, 0, 255), 1, 8);22: double perimeter = arcLength(contours[i], true);
23: cout << "perimeter = " << perimeter << endl;
24: vector<Point> polyCurves;25: double epsilon = 0.01;
26: approxPolyDP(contours[i], polyCurves, epsilon*arcLength(contours[i], true), true); 27: 28: cout << "polygonal curves = " << polyCurves.size() << endl;
29: for (int j = 0; j < polyCurves.size()-1; j++)
30: { 31: Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)); 32: line(imgROI, polyCurves[j], polyCurves[j + 1], color, 3, 8); 33: } 34: 35: line(imgROI, polyCurves[0], polyCurves[polyCurves.size()-1], Scalar(0, 0,255), 3, 8); 36: 37: imshow(name, imgROI);38: cout << "------------------------------" << endl;
39: } 1: 2: bool sortByXAscend(Point a, Point b) { return (a.x<b.x); }
3: bool sortByXDescend(Point a, Point b) { return (a.x>b.x); }
4: bool sortByYAscend(Point a, Point b) { return (a.y<b.y); }
5: bool sortByYDescend(Point a, Point b) { return (a.y>b.y); }