Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cpp version #5

Open
giangdao1402 opened this issue Aug 5, 2024 · 3 comments
Open

Cpp version #5

giangdao1402 opened this issue Aug 5, 2024 · 3 comments

Comments

@giangdao1402
Copy link

Have you complete cpp version ?
Thank you !

@Ashutoshjangam
Copy link

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
Copy link
Author

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

@Ashutoshjangam
Copy link

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants