what is the matlab code to save a textline which is segmented from an image - image

I have a code for simple line segmentation and I can segment the lines (non-overlapping lines), and I can display the lines using the command. Can anybody tell me how to save the lines as .jpg? The code segment for segmenting and displaying the line is shown below
for n=1:Ne
[r,c] = find(L==n);
n1=imagen(min(r):max(r),min(c):max(c));
% I want to save this line in this loop with filename.jpg ( in successive
% numbers like filename_1.jpg, filename_2.jpg and so on )
imshow(~n1);
%%pause(0.5)
pause(4)
end
I want to save the segmented line in this loop with filename.jpg ( in successive
numbers like filename_1.jpg, filename_2.jpg and so on )
Kindly suggest the command for the same

What i understood is you want to save the images with filename and numbered as filename_1.jpg You can write image as
str=strcat('filename_',num2str(n),'.jpg')
imwrite(n1,str)

imwrite(A,filename,fmt);
A is the image array you want to save, the filename is the output file and 'fmt' is the file format.

Related

Access file inside folder, matlab mac

I have about 300 files I would like to access and import in Matlab, all these files are inside 300 folders.
The first file lie in the directory users/matt/Documents/folder_1 with the filename line.csv the 2nd file lie in users/matt/Documents/folder_2 with filename line.csv
So I would like to import the data from the 300 line.csv files in Matlab so I can take the average value. Is this possible? I am using mac osx btw.
I know what do with the .csv file, but I have no clue how to access them efficiently.
This should work: All we are doing is generating the string for every file path using sprintf and the loop index i, and then reading the csv file using csvread and storing the data in a cell array.
for i = 1:300 % Loop 300 times.
% Full path pointing to the csv file.
file_path = sprintf('users/matt/Documents/folder_%d/line.csv', i);
% Read data from csv and store it in a cell array.
data{i} = csvread(file_path);
end
% Do your computations here.
% ...
Remember to replace the 300 by the actual number of folders that you have.

Concatenate RGB channels with a loop 'for'

I have R,G,B image channels of an image and I want to concatenate them to get the colored image using a for loop. Is it possible?
I tried something like below but it gives me an error:
for i=1:3
image=cat(3,img(:,:,i)
end
I already know how to do it without a loop using the command cat : image=cat(3,imgR,imgG,imgB)
I don't see a point in using a loop here... it's very inefficient... especially if you know how many images you want to stack together. The call to cat as you have alluded to in your post at the very end is more than sufficient.
however, if you want to get this working, you have to specify at least two matrices that you want to use to concatenate together to create a new matrix when using cat in order to perform concatenation. You are only specifying one matrix and if you do this, the output will simply be just the channel itself and if you use this code, you will only extract out the blue channel (the last channel).
If you want to retain the concatenations made for each channel, do this instead:
for ii = 1 : 3
image = cat(3, image, img(:,:,ii));
end
This will take the pre-existing variable image and concatenate your matrix with each slice in img and update the variable image.

Increment Serial Number using EXIF

I am using ExifTool to change the camera body serial number to be a unique serial number for each image in a group of images numbering several hundred. The camera body serial number is being used as a second place, in addition to where the serial number for the image is in IPTC, to put the serial number as it takes a little more effort to remove.
The serial number is in the format ###-###-####-#### where the last four digits is the number to increment. The first three groups of digits do not change for each batch I run. I only need to increment that last group of digits.
EXAMPLE
I if I have 100 images in my first batch, they would be numbered:
811-010-5469-0001, 811-010-5469-0002, 811-010-5469-0003 ... 811-010-5469-0100
I can successfully drag a group of images onto my ExifTool Shortcut that has the values
exiftool(-SerialNumber='001-001-0001-0001')
and it will change the Exif SerialNumber Tag on the images, but have not been successful in what to add to this to have it increment for each image.
I have tried variations on the below without success:
exiftool(-SerialNumber+=001-001-0001-0001)
exiftool(-SerialNumber+='001-001-0001-0001')
I realize most likely ExifTool is seeing these as numbers being subtracted in the first line and seeing the second line as a string. I have also tried:
exiftool(-SerialNumber+='1')
exiftool(-SerialNumber+=1)
just to see if I can even get it to increment with a basic, single digit number. This also has not worked.
Maybe this cannot be incremented this way and I need to use ExifTool from the command line. If so, I am learning the command line/powershell (Windows), but am still weak in this area and would appreciate some pointers to get started there if this is the route I need to take. I am not afraid to use the command line, just would need a bit more hand holding then normal for a starting point. I also am learning Linux and could do this project from there but again, not afraid to use it, just would need a bit more hand holding to get it done.
I do program in PHP, JavaScript and other languages so code is not foreign to me. Just experience in writing it for the command-line.
If further clarification is needed, please let me know in the comments.
Your help and guidance is appreciated!
You'll probably have to go to the command line rather than rely upon drag and drop as this command relies upon ExifTool's advance formatting.
Exiftool "-SerialNumber<001-001-0001-${filesequence;$_=sprintf('%04d', $_+1 )}" <FILE/DIR>
If you want to be more general purpose and to use the original serial number in the file, you could use
Exiftool "-SerialNumber<${SerialNumber}-${filesequence;$_=sprintf('%04d', $_+1 )}" <FILE/DIR>
This will just add the file count to the end of the current serial number in the image, though if you have images from multiple cameras in the same directory, that could get messy.
As for using the command line, you just need to rename to remove the commands in the parens and then either move it to someplace in the command line's path or use the full path to ExifTool.
As for clarification on your previous attempts, the += option is used with numbers and with lists. The SerialNumber tag is usually a string, though that could depend upon where it's being written to.
If I understand your question correctly, something like this should work:
1..100 | % {
$sn = '811-010-5469-{0:D4}' -f $_
# apply $sn
}
or like this (if you iterate over files):
$i = 1
Get-ChildItem 'C:\some\folder' -File | % {
$sn = '811-010-5469-{0:D4}' -f $i
# update EXIF data of current file with $sn
$i++
}

Matlab image processing

I have a folder which contains images (100) from the experiment that I did. I also have another folder which contains the background images (100 also) from the detector.
I have written a code that does something like this:
% Define images directory
% Define detector bg directory
% Loop over each frame and do some processing
for a=1:length(image directory)
%read files from directory
bg_corrected_image = frame#-bg_image# % # begins with 1
n=size(image directory)
new_images=zeros(n)
% Now sort through each pixel in bg_corrected image and assign value according to a criterion
for ii=1:size(bg_corrected_image,1)
jj=1:size(bg_corrected_image,2)
pixel=bg_corrected_image(ii,jj);
if pixel>500
pix_mod=0;
elseif pixel<30
pix_mod=0;
else
pix_mod=pixel;
end
new_image(ii,jj)=pix_mod;
end
******************* CODE TO SAVE IMAGE AND NOT OVERWRITE AFTER EACH
ITERATION OF LOOP?
end
What I want to do now is to save each image(frame) after it had gone through the pixel sorting regimen so that I can just sum them all after the loop has ended. I am not too sure what is the best way to do it? I think what I need to do is to create a cell array which saves a "new_image" after each iteration and the code for that should go where I put asteriks. Please note I don't want to save images earlier in my code. Any help much appreciated.
Maybe something like the below - load in all your images to a 3D matrix "imagestack", then process them all, then output them all. Note the vectorization on the pixel replacement here will be much faster than your for-loop iteration over the pixels.
IMAGECOUNT=100;
FILEPATH_IN='images/input/%d.jpg';
FILEPATH_OUT='images/output/%d.jpg';
I=imread(sprintf(FILEPATH,1));
[hei wid]=size(I);
imagestack=zeros(hei,wid,100);
for n=1:IMAGECOUNT
imagestack(:,:,n)=imread(sprintf(FILEPATH_IN,n));
end
imagestack(imagestack>500)=0;
imagestack(imagestack<30)=0;
for n=1:IMAGECOUNT
imwrite(imagestack(:,:,n),sprintf(FILEPATH_OUT,n));
end

Ruby .count operation truncates input file

I want to read a file in and show how large it is. .count is acting like .count! and changing the size of my input file buffer. so now logfile.each doesn't iterate. What's going on?
logfile = open(input_fspec)
puts "logfile size: #{logfile.count} lines"
count will read all the lines from the input in order to do the counting. If you want to read the lines again (e.g. using readline or each) then you will need to call logfile.rewind to move back to the start of the file.
In fact, what count is actually returning is the number of lines that have not been read yet. For example, if you had already read through the file and called count afterwards then it would return 0.
You could do this instead before you even open it:
File.size("input_fspec")

Resources