Jad
Jad

Reputation: 1

Opencv c++ lsb steganography not generating complete image

I am using this code to do lsb steganography, everything seems to work fine but the generated image [[enter image description here](https://i.sstatic.net/DNvJQ.png)](https://i.sstatic.net/kr1vy.png), is cut like not all the column are done by the for loop.

thank you for your help

#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <iostream>
#include <list>
#include <string>
#include <iterator>
#include <algorithm>
#include <vector>
#include <stdlib.h>

using namespace cv;
using namespace std;

void reverseStr(string& str);
static string decimalToBinary(int dec);
static int binaryToDecimal(string binary);



int main()
{
    
    string pixelImg;
    string pixelMessage;
    string pixelDecoded;
    Mat secretMessage = imread("C:\\Users\\jad\\Desktop\\IPA\\message.png");
    Mat carrierImg = imread("C:\\Users\\jad\\Desktop\\IPA\\grayscale_carrier.png");
    Mat encodedImg = carrierImg.clone();
    Mat decodedImg = Mat(secretMessage.rows, secretMessage.cols, secretMessage.type());
   

    if (carrierImg.empty())
    {
        std::cout << "Could not read the image: " << std::endl;
        return 1;
    }
    
    cout << "rows " << carrierImg.rows << " cols " << carrierImg.cols << endl;

    namedWindow("Carrier image");
    imshow("Carrier image", carrierImg);
    namedWindow("Binary message");
    imshow("Binary message", secretMessage);
    int k = waitKey(0);
   
    for (int i = 0; i < carrierImg.rows; i++) {
        for (int j = 0; j < carrierImg.cols; j++) {
            pixelImg = decimalToBinary(encodedImg.at<uchar>(i, j));
            pixelMessage = decimalToBinary(secretMessage.at<uchar>(i,j));
            //pixelImg.replace(pixelImg.size() -1, 1, pixelMessage.substr(0,1));
            pixelImg.at(pixelImg.size() - 1) = pixelMessage.at(pixelMessage.size() - 1);
            //cout << i << ") " << j << ") " << binaryToDecimal(pixelImg) << endl;
            encodedImg.at<uchar>(i, j) = binaryToDecimal(pixelImg);
        }
    }

    for (int i = 0; i < carrierImg.rows; i++) {
        for (int j = 0; j < carrierImg.cols; j++) {
            pixelDecoded = decimalToBinary(encodedImg.at<uchar>(i, j));
            if (pixelDecoded.at(pixelDecoded.size() - 1) == '1') {
                decodedImg.at<uchar>(i, j) = 255;
            } else {
                decodedImg.at<uchar>(i, j) = 0;
            }
            int pixelValue = decodedImg.at<uchar>(i, j);
        //    cout << " j: " << j << " " << pixelValue << " pixel value " << decodedImg.at<uchar>(i, j) << " pixelbinary " << pixelDecoded << endl;
        }
    }

    //cout << "rows " << encodedImg.rows << " cols " << encodedImg.cols << endl;
    
    namedWindow("Encoded image");
    imshow("Encoded image", encodedImg);
    namedWindow("Decoded image");
    imshow("Decoded image", decodedImg);    

    int l = waitKey(0);

    return 0;
}

static int binaryToDecimal(string binary) {
    int number = 0;
   
    for (int i = 0; i <= binary.size(); i++) {
        if (binary.substr(i, 1) == "1") {
            number += pow(2, binary.size() - i - 1);
        }
    }
  
    return number;
}



static string decimalToBinary(int dec) {
    if (dec < 1) {
        return "0";
    }

    string binStr = "";

    while (dec > 0)
    {
        binStr = binStr.insert(0, string(1, (char)((dec % 2) + 48)));

        dec /= 2;
    }

    reverseStr(binStr);
    while (binStr.size() != 8) {
        binStr.push_back('0');
    }
    reverseStr(binStr);
    return binStr;
}

void reverseStr(string& str)
{
    int n = str.length();

    // Swap character starting from two
    // corners
    for (int i = 0; i < n / 2; i++)
        swap(str[i], str[n - i - 1]);
}

I am using this code to do lsb steganography, everything seems to work fine but the generated image [[enter image description here](https://i.sstatic.net/DNvJQ.png)](https://i.sstatic.net/kr1vy.png), is cut like not all the column are done by the for loop.

thank you for your help

Upvotes: 0

Views: 173

Answers (0)

Related Questions