Removing white background from Barcode image(s)

Hello,
I'm currently trying to process a large number of barcodes.
I'm using a function available on the forums called generateBarcodeCode39().
it outputs the generated barcode image(s) and saves each respective figure to jpg in a directory.
My Problem:
The size of my barcodes are different. Some times I will have a string of text that is 4-5 characters long, others more.
this results in different image sizes of barcodes that are saved.
i'm trying to have one size inputted into a formatted excel document, which is why the size of the barcodes are important.
Question:
How would I go about removing just the white background that is generated when creating a matlab figure for each image? I've tried imcrop, and it does work for some cases, but it's not a one size fits all solution.
should i just create different inputs to the imcrop() function that specifies each barcode case and crop it accordingly?

3 comentarios

Rik
Rik el 19 de Nov. de 2020
Can you post a link to that function?
adam campbell
adam campbell el 19 de Nov. de 2020
Editada: adam campbell el 19 de Nov. de 2020
yes. let me attach. I'm actually calling the function in a loop, displaying in a figure, and then saving it off in a jpg.
A section of my code, that takes in a cell array of numbers i want turned into barcodes, creates figure, and saves as image:
for i = 1:length(CellData)
% Generate a logical array containing the barcode
barcodeArray = generateBarcodeCode39(num2str(CellData{i,3}), barWidth, barHeight, appendTerminationMarkers);
% Generate an image of the barcode
figure(i)
fh = imshow(~barcodeArray); % Have to NOT the array, other wise MATLAB displays 1 as black and 0 as white
%not neccessary
if strcmp(class(CellData{i,3}),'char') == 1
saveas(figure(i),[pwd strcat('/Barcodes/',(CellData{i,3})) '.jpg']) ;
else
saveas(figure(i),[pwd strcat('/Barcodes/',num2str(CellData{i,3})) '.jpg']) ;
end
end
this section of code is something i came up with to crop the images, and then save over the existing from the directory. however since they seem to be different size from the function, my current "cropar" variable that acts as RECT in the imcrop() help doesn't work for all cases. see below:
cropar = [130 50 525 190] ;
for i = 1:length(bcdir)
I = imread(bcdir{i,1});
I2 = imcrop(I,cropar);
figure(i)
imshow(I2)
if strcmp(class(bcdir{i,1}),'char') == 1
saveas(figure(i),[pwd strcat('/Barcodes/',(bcdir{i,1})) ]) ;
else
saveas(figure(i),[pwd strcat('/Barcodes/',num2str(bcdir{i,1})) ]) ;
end
end

Iniciar sesión para comentar.

 Respuesta aceptada

Rik
Rik el 20 de Nov. de 2020
The easiest solution is to not add the border in the first place:
barWidth = 3;barHeight = 100;appendTerminationMarkers = false;i=1;CellData{i,3}=123456;
barcodeArray = generateBarcodeCode39('123456', barWidth, barHeight, appendTerminationMarkers);
if isa(CellData{i,3},'char')
imwrite(barcodeArray,fullfile(pwd,'Barcodes',[ CellData{i,3} '.jpg']));
else
imwrite(barcodeArray,fullfile(pwd,'Barcodes',[num2str(CellData{i,3}) '.jpg']));
end

2 comentarios

adam campbell
adam campbell el 20 de Nov. de 2020
thank you very much. this is great!
adam campbell
adam campbell el 20 de Nov. de 2020
Hello, could you please see my new post? I have a similar question that you may be able to help with.
it would be really appreciated.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Convert Image Type en Centro de ayuda y File Exchange.

Productos

Versión

R2019b

Preguntada:

el 19 de Nov. de 2020

Comentada:

el 20 de Nov. de 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by