Transmission of the image using BPSK - matlab

Input image needs to be modulated and transmitted and finally detected using BPSK.
M = 2; %Modulation order 2 for BPSK
randn('state',200); % initializing the randn() function
imdata = imread('lenna.pgm');
bdata = de2bi(imdata);
sizec = size(bdata,1); % height
sizer = size(bdata,2); % width
nbits = sizec*sizer; % number of bits in the image
msg = double(reshape(bdata,nbits,1));
The following code is meant to modulate the input image and display the demodulated output.The resultant output,when i run the code,I get a blank image.Need help in figuring out what I have missed or where I have gone wrong.
%modulate
txpsk = pskmod(msg,M);
%noise
phasenoise = randn(nbits,1)*0.015;
rxpsk = txpsk.*exp(2i*pi*phasenoise);
%demodulate
recovpsk = pskdemod(rxpsk,M);
reshape1rxpsk = reshape(recovpsk,sizec,sizer);
reshape2rxpsk = bi2de(reshape1rxpsk);
finalout= reshape(reshape2rxpsk,size(imdata,1),size(imdata,2));
imshow(finalout)

A PGM file consists of a sequence of one or more PGM images. Each image has a header structure, containing such information, as width, height e.t.c. After modulation you add a noise to your signal. Therefore, the demodulated signal may contain errors. If this errors corrupts header information of image - this may lead to blank image output. Try to comment lines
%phasenoise = randn(nbits,1)*0.015;
%rxpsk = txpsk.*exp(2i*pi*phasenoise);

Related

Save concatenated images in MATLAB

I am trying to concatenate two 256x256 images and save them using imwrite. The saved image is supposed to be 256x512, but when I load the saved image, the size shows to be 343x434x3. How do I solve this?
the code I am using is:
new_name3 = strcat(f_name_image, '\', kk, '_', num2str(ff), '_pair.png');
pair = [orig_im noisy_image]; %concatenating two 256x256 images
imagesc(pair)
f = getframe(gca);
im = frame2im(f);
imwrite(im, new_name3);
Saving the image from the frame can be lossy without configuring additional options. To retain the pixel information save the concatenated image directly from the pair (here Image_Pair) array. Also, the third dimension in 343x434x3 represents the RGB colour channels of the image.
%Grabbing the two images%
Image_1 = imread('Image_1.jpeg');
Image_2 = imread('Image_2.jpeg');
%The file name for the concantenated images%
New_File_Name = "Image_3.jpeg";
%Concatenating the images%
Image_Pair = [Image_1 Image_2];
%Displaying the image pair%
imshow(Image_Pair);
%Saving the image to the "New_File_Name"%
imwrite(Image_Pair, New_File_Name);
%Loading the saved image to see if dimensions are consistent%
Saved_Image = imread('Image_3.jpeg');

DIPimage measure missing argument

I am trying to use DIPimage to get some measurements of each object in an image and I get this error:
Error using dip_measure
DIPlib Error in function dip_Measure.
DIPlib Error in function dip_ImageCheck: Data type not supported
Error in measure (line 209)
data = dip_measure(object_in,gray_in,measurementID,objectIDs,connectivity);
Error in Untitled (line 13)
msr = measure(b, [], ({'size', 'perimeter','podczeckShapes'}))
How can I solve it?
Code:
Image = rgb2gray(imread('pillsetc.png'));
BW = imbinarize(Image);
BW = imfill(BW,'holes');
imshow(BW);
[B,L] = bwboundaries(BW,'noholes');
k = 1;
b = B{k};
y = b(:,2);
x = b(:,1);
msr(k) = measure(BW, [], ({'size', 'perimeter','podczeckShapes'}))
sz = msr.size;
podczeckShapes = podczeckShapes;
One problem with your code is the call to imfill. Because the image has bright values all around the image, it is considered that there's a large object with a hole, and your actual objects are inside this hole. imfill fills the hole, leaving the whole image white.
Instead, I suggest the following code to remove the frame:
Image = rgb2gray(imread('https://i.stack.imgur.com/fmqAF.jpg'));
BW = imbinarize(Image);
BW = BW - bpropagation(false(size(BW)), BW);
Because we used a filter in DIPimage, the BW variable now contains a dip_image object, not a normal MATLAB array. dip_array(BW) extracts the normal MATLAB array that is inside. The dip_image object behaves differently from a MATLAB array. For example, you can display it to an interactive figure window by just typing its name:
BW
Next, we apply labeling so that we know which object ID in the measurement data corresponds to which object:
lab = label(BW);
dipshow(lab,'labels')
Now we can apply the measurement function. If we use BW as input, label will be called on it. Since we already have that result, let's use it directly:
msr = measure(lab, [], {'size', 'perimeter','podczeckShapes'});
Let's examine results for object ID 8, which is the large square:
sz = msr(8).size
square = msr(8).podczeckShapes(1)
triangle = msr(8).podczeckShapes(3)
There are other things you can do with the measurement structure, I suggest you read the documentation. For example, we can remove from it the measurement for the littlest objects, which to me look like noise:
msr = msr(msr.size>100); % remove measurement for noise

read and show raw depth image in matlab

I have a set of .raw depth images. The image format is 500X290 with 32 bytes per pixel. When I open them with IrfanView image viewer I see the depth image correctly like this:
displayed image in IrfanView
Now I want to read and display the same depth image in Matlab. I do like this:
FID=fopen('depthImage.raw','r');
DepthImage = fread(FID,[290,500],'bit32');
fclose(FID);
colormap winter;
imshow(DepthImage);
DepthImage is a 290X500 type double matrix.
what I get from this code is this image:
displayed image in Matlab viewer
when I change fread parameter from 'bit32' to 'bit24' I get this:
displayed image in Matlab with bit24
I guess each element in DepthImage contains 32 bits where each 8 bits corresponds to R,G,B and D values. but how can I read the image correctly and display it like the one in IrfanView?
the raw file: https://drive.google.com/file/d/1aHcRmMKvi5gtodahR5l_Dx8SbK_920c5/view?usp=sharing
There might be an issue with image metadata header, like "date and time of the shot", "camera type". Open your image with notepad++ to check for "date and time". If you upload your original raw image, it will be easier to try things.
Upd: Ok, this is something. Check if it helps
FID=fopen('camera00000000000014167000.raw','r');
DepthImage = fread(FID,290*500*4,'int8');
DepthImageR = DepthImage(1:4:end);
DepthImageG = DepthImage(2:4:end);
DepthImageB = DepthImage(3:4:end);
DepthImageD = DepthImage(4:4:end);
dataR = reshape(DepthImageR, 500,290);
dataG = reshape(DepthImageG, 500,290);
dataB = reshape(DepthImageB, 500,290);
dataD = reshape(DepthImageD, 500,290); % all equal to 64 - useless
figure()
subplot(2,2,1)
image(dataR)
subplot(2,2,2)
image(dataG)
subplot(2,2,3)
image(dataB)
subplot(2,2,4)
data = zeros(500,290,3);
data(:,:,1) = dataR;
data(:,:,2) = dataG;
data(:,:,3) = dataB;
image(data)

Error on Extract hog Features from image sets of different dimensions in Matlab. ''Subscripted assignment dimension mismatch''

How could I perform hog feature extraction from image set of different dimensions? I am getting this error on last section.
''Subscripted assignment dimension mismatch.''
%% Change Directory
cd('C:\Users\inquisitive\Documents\MATLAB\hog_test');
%% Root Folder
rootFolder = fullfile('D:\HGDatabase');
%% Load Imagesets Database Directory
imgSets = imageSet(rootFolder,'recursive');
%%
{ imgSets.Description } % display all labels on one line
[imgSets.Count] % show the corresponding count of images
%% determine the smallest amount of images in a category
minSetCount = min([imgSets.Count]);
imgSets = partition(imgSets, minSetCount, 'randomize');
[imgSets.Count]
%% partioning into training and testing sets
[trainingSet, testSet] = partition(imgSets, 0.2, 'randomize');
*%%CAN'T I SKIP THIS SECTION AND DEFINE hog_16x16 ON LOOP ITSELF WHY I NEED TO PERFORM THIS AT FIRST WITH ONE IMAGE*
img = read(trainingSet(3), 4);
[hog_16x16, vis16x16] = extractHOGFeatures(img,'CellSize',[16 16]);
cellSize = [16 16];
hogFeatureSize = length(hog_16x16);
%%Extracting Hog Features
trainingFeatures = [];
trainingLabels = [];
for hand = 1:numel(trainingSet)
numImages = trainingSet(hand).Count;
features = zeros(numImages, hogFeatureSize, 'single');
for i = 1:numImages
img = read(trainingSet(hand), i);
% Apply pre-processing steps
lvl = graythresh(img);
img = im2bw(img, lvl);
features(i, :) = extractHOGFeatures(img, 'CellSize', cellSize);
end
labels = repmat(trainingSet(hand).Description, numImages, 1);
trainingFeatures = [trainingFeatures; features];
trainingLabels = [trainingLabels; labels ];
end
Other Query:
Which image format work best .jpg or .png? I thought .png would be better? Am i right?
I took some pictures and resize them by thinking that it may train in better way? Am i thinking right or wrong? Or is there nothing to do with different size of input. What would you suggest either to perform in same size or image sets with different dimensions.
CAN'T I SKIP FOLLOWING SECTION AND DEFINE hog_16x16 ON LOOP ITSELF WHY I NEED TO PERFORM THIS AT FIRST WITH ONE IMAGE? If i can skip how to deal with that case
img = read(trainingSet(3), 4);
[hog_16x16, vis16x16] = extractHOGFeatures(img,'CellSize',[16 16]);
cellSize = [16 16];
hogFeatureSize = length(hog_16x16);
You should ensure the dimensions of the image are the same as all images, then your extracted hog_16x16 features will have the same dimensions.

Embedding data from a text file into DWT subband using steganography

I am attempting to embed data from a text file(which contains only numeric data) into LL subband of an image using a steganography. I am getting an error "Error using bitset ASSUMEDTYPE must be an integer type name" in the line of code:
L(ii,jj)=bitset(L(ii,jj),1,stego(ii,jj));
I have attempted to run in debugger but I am having no luck. I think it must be something to do with the data type of L?? I have tried changing image to binary,double etc but I still get this error! Please can someone give me some advice on where I am going wrong?I have a insert my code below
% loading cover image
img=imread('lena.bmp');
image=im2double(img);
% get DWT of image
[LL,LH,HL,HH] = dwt2(image,'haar');
A = importdata('minutiaTest.txt');
I = dec2bin(A,8);
L=LL;
% determine size of LL subband
Mc=size(L,1); %Height
Nc=size(L,2); %Width
% determine size of message object
Mm=size(I,1); %Height
Nm=size(I,2); %Width
for ii = 1:Mc
for jj = 1:Nc
stego(ii,jj)=I(mod(ii,Mm)+1,mod(jj,Nm)+1);
end
end
for ii = 1:Mc
for jj = 1:Nc
L(ii,jj)=bitset(L(ii,jj),1,stego(ii,jj));
end
end
stego_image = idwt2(LL,LH,HL,HH,'haar');
imwrite(uint8(stego_image),'stego.bmp');
figure; imshow(stego_image,title('Stego image'));