Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //=============================================================================
- // NEW IN ASSIGNMENT 2
- //=============================================================================
- //--------------------------------------------------------
- Image Image::clamp(float aMinValue, float aMaxValue) const
- //--------------------------------------------------------
- {
- // Copy the Image instance into a temp one
- Image temp = *this;
- // Process every pixel
- for (std::vector<float>::iterator ite = temp.m_p_image.begin();
- ite != temp.m_p_image.end();
- ++ite)
- {
- // If needed clamp the pixel value between aMinValue and aMaxValue
- if (*ite < aMinValue) *ite = aMinValue;
- else if (*ite > aMaxValue) *ite = aMaxValue;
- }
- return (temp);
- }
- //-----------------------------------------------------------------------------
- std::vector<unsigned int> Image::getHistogram(unsigned int aNumberOfBins) const
- //-----------------------------------------------------------------------------
- {
- vector<unsigned int> p_histogram(aNumberOfBins, 0);
- int size = p_histogram.size();
- int minVal = -0.83714;
- int startFigure = 6.23304 / aNumberOfBins;
- int values[] = { size };
- for (int i = 0; i < aNumberOfBins; i++)
- {
- p_histogram[i] = minVal = +startFigure;
- }
- // Return the histogram
- return (p_histogram);
- }
- //----------------------------------------------------------
- void Image::writeHistogram(const std::string& aFileName,
- unsigned int aNumberOfBins) const
- //----------------------------------------------------------
- {
- std::ofstream output_stream(aFileName);
- if (!output_stream.is_open()) // if the file is not open throw error
- {
- std::string error_message = "Cannot open file (\"";
- error_message += aFileName;
- error_message += "\").";
- throw (error_message); // Throw an error
- }
- else {
- std::ostream_iterator<std::string> output_iterator(output_stream, "\n");
- std::copy(getHistogram(5).begin(), getHistogram(5).end(), output_iterator);
- }
- output_stream.close(); // Close the file
- }
- //---------------------------------------------
- float Image::getSSE(const Image& anImage) const
- //---------------------------------------------
- {
- float sse = 0.00;
- for (float i = 0; i < m_p_image.size; i++)
- {
- float sse = +pow(std::abs(m_p_image[i] - anImage.m_p_image[i]), 2);
- }
- return (sse);
- }
- //---------------------------------------------
- float Image::getMSE(const Image& anImage) const
- //---------------------------------------------
- {
- float mse = getSSE(anImage) / m_p_image.size();
- return (mse);
- }
- //----------------------------------------------
- float Image::getRMSE(const Image& anImage) const
- //----------------------------------------------
- {
- float rmse = sqrt(getMSE(anImage));
- return (rmse);
- }
- //----------------------------------------------
- float Image::getZNCC(const Image& anImage) const
- //----------------------------------------------
- {
- {
- float num, x = 0.0;
- float std_dev1 = getVariance();
- float std_dev2 = anImage.getVariance();
- float avg1 = getAverage();
- float avg2 = anImage.getAverage();
- for (x = 0; x < m_p_image.size(); x++)
- {
- num += ((m_p_image[x] - avg1) * (anImage[x] - avg2)) / (std_dev1 * std_dev2);
- }
- return (num / m_p_image.size());
- }
- }
- //------------------------------------------------
- Image Image::convolve(const float* apKernel) const
- //------------------------------------------------
- {
- {
- // Create a new temp image
- Image temp(getWidth(), getHeight(), 0.0);
- float i, j, m, n, mm, nn;
- int kCenterX, kCenterY; // center index of kernel
- float sum; // temp accumulation buffer
- // find center position of kernel (half of kernel size)
- for (i = 0; i < getWidth(); ++i) // rows
- {
- for (j = 0; j < getHeight(); ++j) // columns
- {
- sum = 0; // init to 0 before sum
- for (m = 0; m < *apKernel; ++m) // kernelY rows !working
- {
- mm = *apKernel - 1 - m; // row index of flipped kernel
- for (n = 0; n < *apKernel; ++n) // kernelX columns !working
- {
- nn = *apKernel - 1 - n; // column index of flipped kernel
- }
- }
- }
- }
- // Return the new image
- return (temp);
- }
- }
- //-----------------------------
- Image Image::meanFilter() const
- //-----------------------------
- {
- // Create a new temp image
- Image temp(getWidth(), getHeight(), 0.0);
- float kernelMean[] = { 1.0 / 9.0, 1.0 / 9.0, 1.0 / 9.0,
- 1.0 / 9.0, 1.0 / 9.0, 1.0 / 9.0,
- 1.0 / 9.0, 1.0 / 9.0, 1.0 / 9.0 };
- // Return the new image from matrix
- return (temp = convolve(kernelMean));
- }
- //---------------------------------
- Image Image::gaussianFilter() const
- //---------------------------------
- {
- // Create a new temp image
- Image temp(getWidth(), getHeight(), 0.0);
- float kernelGaussian[] = {1, 2, 1,
- 2, 4, 2,
- 1, 2, 1 };
- // Return the new image
- return (temp = convolve(kernelGaussian));
- }
- //----------------------------------
- Image Image::laplacianFilter() const
- //----------------------------------
- {
- // Create a new temp image
- Image temp(getWidth(), getHeight(), 0.0);
- float kernelLaplacian[] = { 0, 1, 0,
- 1, -4, 1,
- 0, 1, 0 };
- // Return the new image
- return (temp = convolve(kernelLaplacian));
- }
- //-------------------------------
- Image Image::sobelXFilter() const
- //-------------------------------
- {
- // Create a new temp image
- Image temp(getWidth(), getHeight(), 0.0);
- float kernelSobelX[] = { -1, 0, 1,
- -2, 0, 2,
- -1, 0, 1 };
- // Return the new image
- return (temp = convolve(kernelSobelX));
- }
- //-------------------------------
- Image Image::sobelYFilter() const
- //-------------------------------
- {
- // Create a new temp image
- Image temp(getWidth(), getHeight(), 0.0);
- float kernelSobelY[] = { 1, 2, 1,
- 0, 0, 0,
- -1, -2, -1 };
- // Return the new image
- return (temp = convolve(kernelSobelY));
- }
- //----------------------
- Image Image::abs() const
- //----------------------
- {
- // Create a new temp image
- Image temp(getWidth(), getHeight(), 0.0);
- //=========================
- // Write your own code here
- //=========================
- // Return the new image
- return (temp);
- }
- //------------------------------------
- Image Image::gradientMagnitude() const
- //------------------------------------
- {
- // Create a new temp image
- Image temp(getWidth(), getHeight(), 0.0);
- //=========================
- // Write your own code here
- //=========================
- // Return the new image
- return (temp);
- }
- //----------------------------------------
- Image Image::sharpening(float alpha) const
- //----------------------------------------
- {
- // Create a new temp image
- Image temp(getWidth(), getHeight(), 0.0);
- float kernelSharpening[] = { 0, -1, 0,
- -1, 5, -1,
- 0, -1, 0 };
- // Return the new image
- return (temp = convolve(kernelSharpening));
- }
- //-----------------------------------------------
- Image Image::thresholding(float aThreshold) const
- //-----------------------------------------------
- {
- // Create a new temp image
- Image temp(getWidth(), getHeight(), 0.0);
- for (int i = 0; i < temp.getWidth; i++)
- for (int j = 0; j < temp.getHeight; j++)
- {
- {
- if (temp.getPixel(i, j) >= aThreshold)
- {
- temp.getPixel(i, j) == 0;
- }
- }
- // Return the new image
- return (temp);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement