Định hướng phát triển

Một phần của tài liệu HD4 nhữ quý thơ nghiên cứu, thiết kế mô hình phân loại sản phẩm nông sản theo màu sắc ứng dụng công nghệ xử lý ảnh (Trang 57 - 70)

4. 1 4 Phương hướng giải quyết

4.2 Định hướng phát triển

Việc ứng dụng hệ thống vào quá trình sản xuất có nhiều lợi ích như: Tăng năng suất lao động và chất lượng sản phẩm trong sản xuất. Giúp giảm giá thành sản phẩm để có thể cạnh tranh trên thị trường. Áp dụng công nghệ camera giám sát và phân tích màu trong các công ty chế biến nông sản, gạch men, các công ty dược phẩm,... Áp dụng vào công nghệ nhận biết màu để giám sát sự sinh trưởng và phát triển thực vật. Từ kết quả đạt được và hạn chế, nhóm có một số đề xuất cho hướng phát triển mô hình sau này:

- Hệ thống có thế được mở rộng ở phạm vi thí nghiệm trong các phòng lab, hoặc cũng có thể nghiên cứu sâu để ứng dụng và thay thế các hệ thống đã lỗi thời trong các nhà máy, xí nghiệp.

- Ngoài ra có thể tích hợp them một số module như: module cấp phôi, module phân nhánh…. để từ đó hệ thống có thể hoàn thiện hơn. Bên cạnh đó, có thể thay đổi phân loại đối với từng loại sản phẩm khác nhau từ đó giảm thiểu khả năng chi phí để thay lại toàn bộ hệ thống.

- Tiếp tục nghiên cứu và tìm hiểu về xử lý ảnh để có thể nhận diện được nhiều màu sắc hơn trên sản phẩm phân loại. Cải tiến mô hình phát hiện được sản phẩm ở nhiều mức độ cao hơn, không chỉ có màu mà còn có thể phân loại thêm được cả kích thước của sản phẩm.

- Có thể tích hợp thêm nhiều chức năng nữa như là cấp nguyên liệu và đóng thùng sản phẩm thu được sau khi phân loại.

48

KẾT LUẬN

Qua thời gian nghiên cứu, nhóm đồ án đã phần nào nắm được cơ sở kiến thức, căn bản về xử lý ảnh và áp dụng vào hệ thống phân loại nông sản. Đồ án như một bước đệm nhỏ trong sự phát triển của xu hướng tự động hóa, hiện đại hóa hiện nay, phục vụ trực tiếp cho đời sống sinh hoạt và sản xuất của con người.

Từ sản phẩm “Hệ thống phân loại nông sản ứng dụng xử lý ảnh ” đã nghiên cứu, nhóm đồ án muốn phân tích, hoàn thiện và đưa ra những bước cơ bản trong việc điều khiển tự động bằng phương pháp xử lý ảnh thu được từ camera, đưa ra phương pháp điều khiển. bước đi đầu tiên trong việc liên kết các thiết bị thông minh nhỏ thành một hệ thống lớn. Qua đó, nâng cao khả năng hoạt động của sản phẩm và đáp ứng nhu cầu xã hội ngày nay.

Sau thời gian nỗ lực tìm hiểu, nghiên cứu và hoàn thiện đồ án, đến nay đồ án tốt nghiệp đã được hoàn thành đúng hạn với sự cố gắng của nhóm trong khả năng làm việc chung và phân chia nhiệm vụ về việc tìm hiểu sản phẩm, ứng dụng và chế tạo thành công sản phẩm thực tế. Bằng những kiến thức đã được trang bị ở trường và một số tài liệu tham khảo có liên quan đến vấn đề đang nghiên cứu, nhóm đồ án đã cố gắng trình bày đồ án một cách ngắn gọn và đầy đủ nhất. Tuy nhiên, do trình độ còn hạn chế, chưa có nhiều kinh nghiệm thực tế nên đề tài của nhóm còn có nhiều khiếm khuyết. Qua đây, nhóm đồ án rất mong muốn nhận được ý kiến đóng góp của các thầy cô giáo và các bạn sinh viên để đồ án của nhóm được hoàn thiện hơn và có thể định hướng phát triển tốt sản phẩm trong tương lai.

49

TÀI LIỆU THAM KHẢO

[1] Hoan, Nguyễn Quang. Giáo trình Xử lý ảnh : Học viện công nghệ bưu chính viễn thông, 2006.

[2] Nguyễn Trọng Hiệp, Nguyễn Văn Lâm. Thiết kế chi tiết máy: Nhà xuất bản Giáo Dục, 1999.

[3] Shi, Shin.Emgu CV Essentials.

[4] Ace, Client. Creating a simple Windows Form Application : Kepware Technologies.

[5] Abidi, Andreas Koschan and Mongi. Dectection and Classification of Edges in Color Images. 2005.

[6] [Online] https://www.mathworks.com/help/supportpkg/arduinoio/ug/control- servo-motors.html.

50 PHỤ LỤC Code phần mềm xử lý ảnh: using System; using System.Drawing; using System.Threading; using System.Windows.Forms; using Emgu.CV; using Emgu.CV.Structure; using System.IO.Ports; namespace Image

{ public partial class App : Form

{ private VideoCapture camCapture; private VideoCapture addOnCam; private VideoCapture mainCam; private bool pause;

private Mat camMat = new Mat(); private ColorDialog colorDialog; private Bgr greenMin;

private Bgr greenMax;

private Image<Bgr, byte> mainImage;

public Image<Bgr, byte> inputImage { get; set; } public Image<Bgr, byte> background { get; set; } public Image<Bgr, byte> greenOutput { get; set; } private float counterPixelShape;

private float counterPixelColor; private int shapeThreshold; private int colorThreshold = 60;

private int greenCounter, greenSmallCounter, greenBigCounter; private int redCounter, redSmallCounter, redBigCounter; private bool isMainCam = true;

private bool turnOnCam = false; private bool isConnected = false;

51

private ColorType objectColor;

private ShapeType objectShape; // Serial port static SerialPort serialPort;// Data signal private const int GREEN_SMALL = 1;

private const int GREEN_BIG = 2; private const int RED_SMALL = 3;

private const int RED_BIG = 4;// Image path from local private static string CONNEXT_IMAGE_PATH = @"C:\Users\84395\source\repos\Image\Image\Resources\Connected.png" ;

private static string DISCONNECT_IMAGE_PATH = @"C:\Users\84395\source\repos\Image\Image\Resources\Dicconnected.p ng";// Instance bitmap from image path

private Bitmap connectImage = new Bitmap(CONNEXT_IMAGE_PATH);

private Bitmap disconnectImage = new Bitmap(DISCONNECT_IMAGE_PATH); // Signal State

private SignalState _signalState; public enum ColorType {

None = 0, Green = 1, Red = 2 } public enum ShapeType {

None = 0, Small = 1, Big = 2} public enum SignalState {

PreSignal, InSignal, LateSignal} public App(){

_signalState = SignalState.PreSignal; InitializeComponent();

CheckForIllegalCrossThreadCalls = false; mainCam = new VideoCapture(0);

addOnCam = new VideoCapture(1);

portNames = SerialPort.GetPortNames(); if (portNames.Length > 0) {

Port.Text = portNames[0]; serialPort = new SerialPort(); serialPort.PortName = portNames[0];

52

serialPort.BaudRate = 9600; }} #region Start Stop Camera// Camera recording

private void CamCapture_ImageGrabbed(object sender, EventArgs e) {

if (!pause)return;

camCapture.Read(camMat);

mainImage = camMat.ToImage<Bgr, byte>();

mainImage.Resize(0.4, Emgu.CV.CvEnum.Inter.Cubic); MainCamera.Image = mainImage.ToBitmap();}

// Button clicked to turn on, off camera

private void StartButton_Click(object sender, EventArgs e) { pause = !pause; if (pause){ StartButton.Text = "Pause"; StartButton.BackColor = Color.Thistle;} else{ StartButton.Text = "Start"; StartButton.BackColor = Color.Coral;} Console.WriteLine(pause); new Thread(PlayCamera).Start(); } // Turn on, off camera

private void PlayCamera{ if (pause){ if (camCapture == null){ turnOnCam = true; camCapture = mainCam;} camCapture.ImageGrabbed+= CamCapture_ImageGrabbed; camCapture.Start(); } else { turnOnCam = false; camCapture.Pause(}}

53 #endregion

#region Image Processing // Set picture box by bitmap

private void SetImage(PictureBox pic, Bitmap bitmap){ pic.Image = bitmap;}

// Capture background image

private void BgCapture_Click(object sender, EventArgs e) { if (camCapture == null){ MessageBox.Show("Turn on camera"); return;} SetImage(Bg, mainImage.Bitmap); background = mainImage.Clone(); greenOutput = mainImage.Clone();}

// Compare button click to campare color and shape private void Compare_Click(object sender, EventArgs e){ CaptureObject();}

// Capture object

private void CaptureObject(){ if (Bg.Image == null){ MessageBox.Show("Take background"); return;} if (camCapture == null){ MessageBox.Show("Turn on camera"); return;} SetImage(Input, mainImage.Bitmap); inputImage = mainImage.Clone(); greenOutput = mainImage.Clone(); new Thread(BackgroundSubtraction).Start();} // Background subtraction to find object

private void BackgroundSubtraction(){ counterPixelColor = 0;

counterPixelShape = 0;

54

difference = difference.ThresholdBinary(new Bgr(70, 70, 70), new Bgr(255, 255, 255));

Bitmap diffBitmap = difference.ToBitmap(); Bitmap output = greenOutput.Bitmap;

for (int x = 0; x < diffBitmap.Width; x++){ for (int y = 0; y < diffBitmap.Height; y++){ Color pixel = diffBitmap.GetPixel(x, y); if(pixel.R != 0 || pixel.B != 0 || pixel.G != 0){ diffBitmap.SetPixel(x, y, Color.White); counterPixelShape++;} else{ diffBitmap.SetPixel(x, y, Color.Black); output.SetPixel(x, y, Color.Black);}}} Console.WriteLine(diffBitmap.Width*diffBitmap.Height); greenOutput = new Image<Bgr, byte>(output);

BgSubtract.Image = diffBitmap;

CompareShape(counterPixelShape, diffBitmap.Width * diffBitmap.Height);

new Thread(DetectColor).Start();} // Detect color in color range

private void DetectColor(){ counterPixelColor = 0;

greenOutput._SmoothGaussian(5);

var mask = greenOutput.InRange(greenMin, greenMax); greenOutput.SetValue(new Bgr(150, 150, 150), mask); var bitmap = greenOutput.Bitmap;

for (int x = 0; x < bitmap.Width; x++){ for (int y = 0; y < bitmap.Height; y++){ Color pixel = bitmap.GetPixel(x, y);

if (pixel.R == 150 && pixel.G == 150 && pixel.B == 150){ counterPixelColor++;

bitmap.SetPixel(x, y, Color.White);} else{

55

GreenChanel.Image = greenOutput.ToBitmap();

CompareColor(counterPixelColor, counterPixelShape); color.Text = counterPixelColor.ToString();

shape.Text = counterPixelShape.ToString();

CounterProduct();}// Compare shape by shape threshold private void CompareShape(float pixelCount, float totalPixel){ float ratio = (pixelCount / totalPixel) * 100f; Console.WriteLine(ratio.ToString());

if (ratio >= shapeThreshold){ objectShape = ShapeType.Big;} else{

objectShape = ShapeType.Small;}}//Compare value color by color range private void CompareColor(float pixelCount, float totalPixel){

float ratio = (pixelCount / totalPixel) * 100f; if (ratio >= colorThreshold){

objectColor = ColorType.Green;} else{

objectColor = ColorType.Red;}// Counter product and show in screen private void CounterProduct(){

switch (objectColor){ case ColorType.None: break; case ColorType.Green: greenCounter++; GreenText.Text = greenCounter.ToString(); switch (objectShape) { case ShapeType.None: break; case ShapeType.Small: greenSmallCounter++; GreenSmallText.Text = greenSmallCounter.ToString(); if (serialPort != null) serialPort.Write("1"); break;

56 case ShapeType.Big: greenBigCounter++; GreenBigText.Text = greenBigCounter.ToString(); if (serialPort != null) serialPort.Write("2"); break; default: break;} break; case ColorType.Red: redCounter++; RedText.Text = redCounter.ToString(); switch (objectShape){ case ShapeType.None: break; case ShapeType.Small: redSmallCounter++; RedSmallText.Text = redSmallCounter.ToString(); if (serialPort != null) serialPort.Write("3"); break; case ShapeType.Big: redBigCounter++; RedBigText.Text = redBigCounter.ToString(); if (serialPort != null) serialPort.Write("4"); break; default: break;} break; default: break;}} #endregion

57

private void ConnectButton_Click(object sender, EventArgs e){ isConnected = !isConnected;

if (isConnected) Connect(); else

Disconnect();} //Connect to arduino private void Connect(){

isConnected = true; serialPort.Open(); serialPort.DataReceived += SerialPort_DataReceived; ConnectButton.Image = disconnectImage; StatusText.Text = "Connected";} // Disconnect to arduino

private void Disconnect(){ isConnected = false; serialPort.Close();

ConnectButton.Image = connectImage; StatusText.Text = "Disconnected";} //Recieved Data From Arduino

private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e){

string inputValue = serialPort.ReadLine(); if(inputValue != null){

if(inputValue == "0\r"){ UpdateSignalState(true);} else{

UpdateSignalState(false);}}} // Update Signal State

private void UpdateSignalState(bool decision){ switch (_signalState){

case SignalState.PreSignal: if (decision){

_signalState = SignalState.InSignal;} break;

58 case SignalState.InSignal: CaptureObject(); _signalState = SignalState.LateSignal; break; case SignalState.LateSignal: if (!decision){ _signalState = SignalState.PreSignal;} break;}} // Switch camera

private void SwapCamera_Click(object sender, EventArgs e){ if (!turnOnCam) return;

isMainCam = !isMainCam;

if (isMainCam) camCapture = mainCam; else camCapture = addOnCam;}

#endregion

#region Set Color, Shape Range// Set min color value private void MinColor_Click(object sender, EventArgs e){ colorDialog = new ColorDialog();{

MinColor.BackColor = colorDialog.Color; greenMin = new Bgr(colorDialog.Color);}} // Set max color value

private void MaxColor_Click(object sender, EventArgs e){ colorDialog = new ColorDialog();

if (colorDialog.ShowDialog() == DialogResult.OK){ MaxColor.BackColor = colorDialog.Color;

greenMax = new Bgr(colorDialog.Color);}} // Set shape threshold by trackbar

private void ShapeSlider_Scroll(object sender, EventArgse){ shapeThreshold = (int)ShapeSlider.Value;

ShapeNum.Value = shapeThreshold;}//Set shape threshold by number box private void ShapeNum_ValueChanged(object sender, EventArgs e){ shapeThreshold = (int)ShapeNum.Value;

ShapeSlider.Value = shapeThreshold;} #endregion}}

Một phần của tài liệu HD4 nhữ quý thơ nghiên cứu, thiết kế mô hình phân loại sản phẩm nông sản theo màu sắc ứng dụng công nghệ xử lý ảnh (Trang 57 - 70)

Tải bản đầy đủ (PDF)

(70 trang)