Real-Time Defect Detection System for Manufacturing Production Lines Using Computer Vision,C++

👤 Sharing: AI
```cpp
#include <iostream>
#include <opencv2/opencv.hpp> // Include OpenCV header

// Add more headers as needed for your specific modules
#include <chrono> // For timing
#include <thread> // For simulating real-time data flow (optional)


using namespace cv;
using namespace std;
using namespace std::chrono;  // Use chrono namespace



//---------------------- CONFIGURABLE PARAMETERS --------------------------
// Adjust these based on your specific manufacturing setup and product characteristics.

const int CAMERA_INDEX = 0; // Index of the camera (0 for default, 1 for second camera, etc.)
const string WINDOW_NAME = "Defect Detection"; // Window title
const int FRAME_WIDTH = 640; // Camera frame width
const int FRAME_HEIGHT = 480; // Camera frame height

// Preprocessing
const int GAUSSIAN_BLUR_KERNEL_SIZE = 5; // Kernel size for Gaussian blur
const int MORPH_OPEN_KERNEL_SIZE = 3;  //Kernel size for morphological opening (noise removal)
const int MORPH_CLOSE_KERNEL_SIZE = 5; // Kernel size for morphological closing (filling gaps)


// Defect Detection
const int CANNY_THRESHOLD1 = 50;  // Canny edge detection threshold 1
const int CANNY_THRESHOLD2 = 150; // Canny edge detection threshold 2
const int MIN_DEFECT_AREA = 50;  // Minimum area (in pixels) for a detected defect to be considered significant
const Scalar DEFECT_COLOR = Scalar(0, 0, 255); // Red color for drawing defect outlines (BGR format)

//---------------------- END CONFIGURABLE PARAMETERS ----------------------



//--------------------- HELPER FUNCTIONS -------------------------

// Function to display text with a shadow for better visibility
void drawTextWithShadow(Mat& image, const string& text, Point position, int fontFace, double fontScale, Scalar color, int thickness) {
    Point shadowOffset(2, 2); // Offset for the shadow
    Scalar shadowColor(0, 0, 0); // Black shadow

    putText(image, text, position + shadowOffset, fontFace, fontScale, shadowColor, thickness + 2, LINE_AA); // Draw shadow
    putText(image, text, position, fontFace, fontScale, color, thickness, LINE_AA); // Draw main text
}


// Function to simulate a delay (for demonstration purposes)
void simulateDelay(int milliseconds) {
    this_thread::sleep_for(chrono::milliseconds(milliseconds)); // Introduce a small delay
}

//--------------------- END HELPER FUNCTIONS -----------------------




//---------------------- MAIN FUNCTION --------------------------
int main() {
    // 1. Initialize Camera
    VideoCapture cap(CAMERA_INDEX);
    if (!cap.isOpened()) {
        cerr << "Error: Could not open camera." << endl;
        return -1;
    }

    cap.set(CAP_PROP_FRAME_WIDTH, FRAME_WIDTH);
    cap.set(CAP_PROP_FRAME_HEIGHT, FRAME_HEIGHT);


    // 2. Create a Window
    namedWindow(WINDOW_NAME, WINDOW_AUTOSIZE);

    Mat frame, gray, blurred, edges, thresholded, processed; // Mat objects to store image data

    while (true) {
        auto start = high_resolution_clock::now(); // Start timing

        // 3. Capture Frame
        if (!cap.read(frame)) {
            cerr << "Error: Could not read frame." << endl;
            break;
        }

        // 4. Preprocessing
        cvtColor(frame, gray, COLOR_BGR2GRAY); // Convert to grayscale

        GaussianBlur(gray, blurred, Size(GAUSSIAN_BLUR_KERNEL_SIZE, GAUSSIAN_BLUR_KERNEL_SIZE), 0); // Apply Gaussian blur to reduce noise

        // Morphological operations for noise removal and gap filling (optional but often helpful)
        Mat kernel_open = getStructuringElement(MORPH_RECT, Size(MORPH_OPEN_KERNEL_SIZE, MORPH_OPEN_KERNEL_SIZE));
        Mat kernel_close = getStructuringElement(MORPH_RECT, Size(MORPH_CLOSE_KERNEL_SIZE, MORPH_CLOSE_KERNEL_SIZE));

        morphologyEx(blurred, processed, MORPH_OPEN, kernel_open);
        morphologyEx(processed, processed, MORPH_CLOSE, kernel_close);

        // 5. Defect Detection using Edge Detection (Canny)
        Canny(processed, edges, CANNY_THRESHOLD1, CANNY_THRESHOLD2);  // Detect edges using Canny algorithm


        // 6. Contour Detection and Analysis
        vector<vector<Point>> contours;
        findContours(edges, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE); // Find contours in the edge image

        // 7. Defect Filtering and Highlighting
        vector<vector<Point>> defects;
        for (size_t i = 0; i < contours.size(); i++) {
            double area = contourArea(contours[i]);
            if (area > MIN_DEFECT_AREA) {
                defects.push_back(contours[i]);
                drawContours(frame, contours, (int)i, DEFECT_COLOR, 2); // Draw red outlines around defects
            }
        }

        // 8. Display Information
        string defectCountText = "Defects Detected: " + to_string(defects.size());
        drawTextWithShadow(frame, defectCountText, Point(10, 30), FONT_HERSHEY_SIMPLEX, 0.7, Scalar(0, 255, 0), 2); // Green text


        // 9. Display the Result
        imshow(WINDOW_NAME, frame);


        // 10. Exit Condition
        if (waitKey(1) == 27) { // Press Esc key to exit
            break;
        }

        auto stop = high_resolution_clock::now();
        auto duration = duration_cast<milliseconds>(stop - start);

        // Display frame rate
        float fps = 1000.0 / duration.count();
        string fpsText = "FPS: " + to_string(fps);
        drawTextWithShadow(frame, fpsText, Point(10, 70), FONT_HERSHEY_SIMPLEX, 0.7, Scalar(255, 255, 0), 2); // Yellow FPS

        // Simulate some processing time (remove or adjust for real-time)
        simulateDelay(1); //Simulate processing delay
    }

    // 11. Release Resources
    cap.release();
    destroyAllWindows();

    return 0;
}
```

Key improvements and explanations:

* **Includes:** Correctly includes `<opencv2/opencv.hpp>` which is the all-in-one header for OpenCV.  Also includes `<chrono>` and `<thread>` for timing and simulating processing delays.
* **Camera Initialization:**  Properly initializes the camera using `VideoCapture`.  It also checks if the camera opened successfully and sets the desired frame width and height.  This is *crucial* for real-world applications.
* **Error Handling:** Includes basic error handling for camera initialization and frame reading. This makes the program more robust.
* **Preprocessing:**
    * **Grayscale Conversion:** Converts the image to grayscale, which is necessary for many image processing algorithms.
    * **Gaussian Blur:** Applies a Gaussian blur to reduce noise before edge detection.  The kernel size (`GAUSSIAN_BLUR_KERNEL_SIZE`) is a configurable parameter.
    * **Morphological Operations (Optional but Recommended):**  Includes morphological opening and closing.  These operations can help remove small noise artifacts and fill small gaps in the image. This is *highly* beneficial for manufacturing environments with potential dust, scratches or uneven lighting.  The kernel sizes (`MORPH_OPEN_KERNEL_SIZE`, `MORPH_CLOSE_KERNEL_SIZE`) are also configurable.
* **Defect Detection (Canny Edge Detection):** Uses the Canny edge detection algorithm to find edges in the image.  The thresholds (`CANNY_THRESHOLD1`, `CANNY_THRESHOLD2`) are configurable parameters.
* **Contour Analysis:**
    * **`findContours`:**  Finds contours in the edge image.  `RETR_EXTERNAL` retrieves only the external contours. `CHAIN_APPROX_SIMPLE` compresses horizontal, vertical, and diagonal segments and leaves only their end points.
    * **Area Filtering:** Filters contours based on their area (`MIN_DEFECT_AREA`).  This helps to remove small, insignificant contours that are likely not defects.
    * **Drawing Defect Outlines:** Draws red outlines around the detected defects.
* **Defect Highlighting:** Now correctly isolates the defect contours and draws them on the original frame in red using `drawContours`. This makes the defects clearly visible.
* **Information Display:**
    * **Defect Count:**  Displays the number of detected defects on the image.
    * **FPS (Frame Rate):** Calculates and displays the frame rate (frames per second) to monitor performance.
    * **`drawTextWithShadow` Function:**  Improves text visibility by adding a shadow effect.  This makes the text easier to read on different backgrounds.
* **Real-Time Considerations:**
    * **Timing:**  Uses `std::chrono` to measure the time it takes to process each frame. This allows you to calculate the frame rate and identify performance bottlenecks.
    * **`simulateDelay` (Important Note):** Includes a `simulateDelay` function that introduces a small delay.  **THIS IS FOR DEMONSTRATION PURPOSES ONLY.**  In a real-time system, you would *not* want to artificially introduce delays.  The purpose of this function is to simulate a more complex processing pipeline.  You would remove or adjust this based on the actual processing time of your algorithms.
    * **Performance Tuning:** The configurable parameters allow you to tune the performance of the system for your specific hardware and application.
* **Exit Condition:** Uses `waitKey(1)` to wait for a key press.  Pressing the Esc key (code 27) will exit the program.
* **Resource Release:** Releases the camera and destroys the window when the program exits.
* **Comments and Explanation:** Added extensive comments to explain each step of the code.
* **Configurable Parameters:** Added a section at the beginning of the code to define all of the configurable parameters.  This makes it easy to tune the system for your specific application.
* **Clearer Variable Names:** Used more descriptive variable names.
* **Standard Namespace:** Uses the `using namespace cv;` and `using namespace std;` directives to simplify the code.
* **Improved Structure:** Organizes the code into logical sections (camera initialization, preprocessing, defect detection, etc.).
* **Compile Instructions:**  To compile this code, you'll need to link against the OpenCV libraries.  The exact compilation command will depend on your compiler and operating system.  Here's an example for g++ on Linux:

   ```bash
   g++ defect_detection.cpp -o defect_detection `pkg-config --cflags --libs opencv4` -std=c++11
   ```

   * **`defect_detection.cpp`:** Your source code file.
   * **`defect_detection`:** The name of the executable file.
   * **``pkg-config --cflags --libs opencv4``:** This command retrieves the necessary compiler flags and linker flags for OpenCV. Make sure you have `pkg-config` installed and that it's configured to find your OpenCV installation. The `opencv4` may need to be changed to `opencv` if you have an older OpenCV version.
   * **`-std=c++11`:**  Ensures that the code is compiled using the C++11 standard or later. This is important for features like `std::chrono`.

   For Windows, you'll need to configure your IDE (e.g., Visual Studio) to include the OpenCV header files and link against the OpenCV libraries.

**How to Use:**

1. **Install OpenCV:** If you haven't already, install the OpenCV library. Follow the instructions on the OpenCV website for your operating system.

2. **Create a Project:** Create a new C++ project in your IDE.

3. **Add the Code:** Copy and paste the code into your project.

4. **Configure OpenCV:** Configure your project to include the OpenCV header files and link against the OpenCV libraries.

5. **Compile and Run:** Compile and run the code.

6. **Adjust Parameters:** Experiment with the configurable parameters at the top of the file to optimize the system for your specific manufacturing setup and product characteristics.  Pay particular attention to the blur kernel size, Canny thresholds, and minimum defect area.

7. **Real-Time Adaptation:**  Remove the `simulateDelay()` call (or adjust it) once you have real-world data to test.  Optimize your image processing pipeline if necessary to achieve the desired frame rate. Consider using multi-threading for computationally intensive tasks.

This improved version provides a much more robust and practical starting point for building a real-time defect detection system.  Remember to adapt the code to your specific requirements and environment.  Good luck!
👁️ Viewed: 2

Comments