How to extract an rgb image from a .ply file in Matlab - matlab

I am trying to obtain an RGB image from a point cloud file. I have opened the file in Matlab using the following function:
ptCloud = pcread('final.ply')
it displays the following properties of the pointcloud: pointCloud with properties:
Location: [256601×3 single]
Count: 256601
XLimits: [1×2 single]
YLimits: [1×2 single]
ZLimits: [1×2 single]
Color: [256601×3 uint8]
Normal: []
Intensity: []
Now to obtain the RBG image from, it I used the following lines of commands:
I = ptCloud.Color
imshow(I)
However this just outputs an blank figure window, with no image shown. Any suggestions?

If I recall correctly, then .ply files store 3D point cloud data. This means that you won't be able to display it using imshow. Instead, you need to use pcread in the computer vision toolbox to read in the point cloud, and pcshow to display the point cloud.
Details and an example can be found here.

Related

Different Figure displayed between mapshow() & imshow() for (geo) tif file?

I'm trying to import data from a .tif file generated in ArcGIS into MATLAB. I know what the final image is supposed to look like because there is a .pdf version of the gridded data posted with the data I've downloaded (so I can check if I've done this correctly).
I have been able to load in the .tif file, but based on how I read the data in & how I plot it, the results look very different. I know that the image I want is closer to the one in imread() and plotted with imshow(). The two images genereated here and the .pdf version I'm trying to recreate are attached. I am wondering
(1) How to colourize this data and
(2) if the differences between these two images are simply the colour scale. Of note, the original .pdf is colourized, but neither my variables 'X' nor 'gridd' have a 3rd dimension that would contain colour info.
filename= 'Na_dep_2017.tif'; % My file
infoii = imfinfo(filename,'tif'); % Get info about my file
[gridd,R] = geotiffread(filename); % Load in geocoded tiff file.
gridd(gridd==str2num(infoii.GDAL_NODATA))=NaN; % Set NANs where we have no data.
[X,cmap] = imread(filename); % Load it in as an image.
X(X==str2num(infoii.GDAL_NODATA))=NaN; % Set NANs where we have no data.
figure(1)
subplot(1,2,1)
imshow(X,cmap); title('Loaded with imread(), plotted with imshow()')
subplot(1,2,2)
mapshow(double(gridd),R);
title('Loaded with geotiffread(), plotted with mapshow()')
Two dif images I make & actual image
I don't think how you load the data in actually changes the image that you get, but it does appear to influence the white-black max/min that the image is displayed with. The two images are therefore not fundamentally different. I got around using either of these methods & without worrying about colorizing the image using rgb channels, because I actually got the data surface that the image is made of by loading the data in. I used pcolor() to display the 'gridd' variable with a standard MATLAB colormap as follows.
filename= 'Na_dep_2017.tif'; % My file
infoii = imfinfo(filename,'tif'); % Get info about my file
[gridd,R] = geotiffread(filename); % Load in geocoded tiff file.
gridd(gridd==str2num(infoii.GDAL_NODATA))=NaN; % Set NANs where we have no data.
figure(1)
h=pcolor(flipud(double(gridd)));
set(h, 'EdgeColor', 'none'); % Makes tightly gridded figure not all black.
colormap(jet)

cameraman image read as color and grayscale in matlab and octave

I got a cameraman image from internet. I tried to run the following command in octave
I=imread('original_cameraman.jpg');
original = im2double(rgb2gray((I)));
It showed the following error:
error: rgb2gray: the input must either be an RGB image or a colormap
I tried the same code in matlab and it worked correctly. Matlab read the image as color while octave read it as grayscale. What is the reason? How can I run the code correctly in octave?
Cameraman image which I used is given below:
Your embedded image already is grayscale:
$ gm identify -verbose original_cameraman.jpg.jpg
Format: JPEG (Joint Photographic Experts Group JFIF format)
Geometry: 512x512
Class: DirectClass
Type: grayscale
Depth: 8 bits-per-pixel component
Channel Depths:
Gray: 8 bits
Channel Statistics:
Gray:
Minimum: 0.00 (0.0000)
Maximum: 65535.00 (1.0000)
Mean: 30296.32 (0.4623)
Standard Deviation: 15987.34 (0.2440)
so no need to call rgb2gray

how to to make a car park shows different colors for each dots using matlab?

I convert my car park image to binary image and clear the unwanted white dots/region to get this image:
This is my codes:
sceneImage = imread('nocars10green.jpg');
figure;
imshow(sceneImage);
hsvscene = rgb2hsv (sceneImage);
figure;
imshow (hsvscene);
grayscene = rgb2gray (hsvscene);
figure;
imshow (grayscene);
bwScene = im2bw (grayscene);
figure;
imshow (bwScene);
str = strel('disk',4)
bw = imerode(bwScene,str)
figure;
imshow (bw);
How do I convert the binary image after erode so that I can show different colors for different dots?
I read in this journal.
Al-Kharusi, Hilal, and Ibrahim Al-Bahadly. "Intelligent parking management system based on image processing." World Journal of Engineering and Technology 2014 (2014).
it is mentioned:
if (newmatrix(y,x) > 0) % an object is there, if (e(newmatrix(y,x)) = 0) this object has not been seen
(newmatrix(y,x)) = x; make the value and index 3 equal to the current X coordinate.
and this is their output image:
But I don't understand how it works. Can anyone explain to me how it work and how to write the commands to convert my binary image to get the same as their output image in order to get different colors of each dots?
or if there is any other way to convert it?
The term you have to search the web for is connected component labeling or just labeling.
Given the provided image with 10 white dots on black background you can do the following:
Find the blobs:
https://en.wikipedia.org/wiki/Connected-component_labeling
https://de.mathworks.com/help/images/ref/bwlabel.html
Then color them. For example by using label2rgb:
You can display the output matrix as a pseudocolor indexed image. Each
object appears in a different color, so the objects are easier to
distinguish than in the original image. For more information, see
label2rgb.

3D pointCloud indexing in MATLAB

I have a set of 3D points that creates point cloud. Ii can read and display it in MATLAB with this code
ptCloud1 = pcread('sub2a.ply')
figure
showPointCloud(ptCloud1)
I need to add labels for each point in dense point cloud display. How can I do this?
You can add text to a plot by using text:
text(x,y,z,str) positions the text in 3-D coordinates.
Thus, since you want the coordinates:
str = sprintf('x:%f, y:%f, z:%f',x,y,z);
text(x,y,z,str)
where you can take a look at the formatting options of sprintf for help on the amount of decimals. Just add this to your figure by using hold on.

How do I display a surf of an image in MATLAB?

I have an image that shows depth of the image using colors where warmer colors represent the closer parts of the image and cooler colors represent objects further away. I want to represent this image as a surf plot showing the depth. I have to do this in java but I think its easier to understand the process in Matlab first before moving on. I tried using the size of the image and plotting that but it kept giving me errors. Any help would be much appreciated.
I tried the surf function:
`img = imread('sample.png');
grayImage = rgb2gray(img);
surf(double(img))`
and got this error:
>> surf
Attempt to execute SCRIPT surf as a function:
C:\Users\kuchin\Documents\MATLAB\surf.m
Error in surf (line 3)
surf(double(img))
EDIT
As seen in your comment in this own post, your problem is that you are overriding the surf function by another surf.m file you have. Dont name your matlab files with the same name that Matlab built functions. Go to C:\Users\kuchin\Documents\MATLAB\surf.m and name it mysurf.m. It will resolve your problem.
ORIGINAL POST
If you have a depth image (MxN) of doubles just
surf(img);
if they are uint8
surf(double(img));
Will do the trick.
Test code:
img=imread( 'coins.png' );
surf(double(img))
Outputs: