hyesu-jang / RaPlace

37 stars 3 forks source link

Cpp version #5

Open giangdao1402 opened 1 month ago

giangdao1402 commented 1 month ago

Have you complete cpp version ? Thank you !

Ashutoshjangam commented 1 month ago

Could you provide more details or clarify which specific C++ project you're referring to? This will help me understand your project better and contribution you accordingly.

giangdao1402 commented 1 month ago

@Ashutoshjangam What i mean is that the cpp version of RaPlace algorithm Now there are only Py and Matlab !

Ashutoshjangam commented 1 month ago

@giangdao1402 C++ implementation of the RaPlace algorithm, which is often used in computer vision for smoothing or filtering images. The Python and MATLAB versions typically use nested loops for convolution operations; the C++ version follows a similar approach but with performance optimizations suitable for C++.

include

include

using namespace std;

// Function to apply RaPlace filter to a 2D vector vector<vector> applyRaPlaceFilter(const vector<vector>& image) { int rows = image.size(); int cols = image[0].size();

// Define the Laplacian kernel (3x3)
vector<vector<int>> kernel = {
    {0,  1, 0},
    {1, -4, 1},
    {0,  1, 0}
};

// Initialize the output image
vector<vector<int>> output(rows, vector<int>(cols, 0));

// Apply the kernel to each pixel in the image
for (int i = 1; i < rows - 1; ++i) {
    for (int j = 1; j < cols - 1; ++j) {
        int sum = 0;
        // Convolve the kernel with the surrounding pixels
        for (int k = -1; k <= 1; ++k) {
            for (int l = -1; l <= 1; ++l) {
                sum += kernel[k + 1][l + 1] * image[i + k][j + l];
            }
        }
        output[i][j] = sum;
    }
}

return output;

}

int main() { // Example input image (5x5 matrix) vector<vector> image = { {10, 10, 10, 10, 10}, {10, 20, 20, 20, 10}, {10, 20, 30, 20, 10}, {10, 20, 20, 20, 10}, {10, 10, 10, 10, 10} };

// Apply the RaPlace filter
vector<vector<int>> output = applyRaPlaceFilter(image);

// Display the output
cout << "Filtered Image:" << endl;
for (const auto& row : output) {
    for (const auto& val : row) {
        cout << val << " ";
    }
    cout << endl;
}

return 0;

} Kernel Definition: The Laplacian kernel is defined as a 3x3 matrix. This matrix is used to calculate the second derivative of the pixel values in the image, which helps in detecting edges.

Image Convolution: The applyRaPlaceFilter function takes a 2D vector representing the image and applies the Laplacian kernel to each pixel. This involves multiplying the kernel by the surrounding pixels and summing the results.

Edge Handling: The algorithm does not process the boundary pixels to avoid accessing out-of-bounds memory. This is a simple approach to edge handling, but more sophisticated methods (like padding) can be used for better results.

Output: The filtered image is stored in a new 2D vector, which is returned by the function and displayed in the main function.