DDSM Database Conversion image format--ljpeg to PNG format

Source: Internet
Author: User
Tags ftp file ftp site

Digital Database for screening mammography (DDSM) is a very large breast image database with more than 10,000 images, but the image format is ljpeg, and existing image software (such as Photoshop, Accdsee, Windows-brought image viewing software), and programming software such as MATLAB, are unreadable and need to be converted to other common formats for use. I found a lot of methods on the Internet, and it was unsuccessful after the trial, including the founder of the database--a program written by the University of South Florida itself [1], a medical image format conversion software xmedcon[2]. The last successful approach was to use a complete program written by Dr. Chris Rose of the University of Manchester , which made some modifications to his program, successfully converting the image format into PNG format. His program chain to interview Http://microserf.org.uk/academic/Software.html

from the links given above to download the program, there is a user's manual tells you how to use this program. This program is written in Ruby and needs to be run under Cygwin, and the user manual shows you how to install Cygwin and other required tools. The program workflow is the user manually enter the image name, the program download the image from FTP, and then after a few steps to convert, and eventually converted to PNG format. I run this program in accordance with the user manual is not successful, with VS2013 open Get-ddsm-mammo file to view the source code, found to download the image from the FTP link out of the problem, made a change, the final run successfully. My revised program is as follows:

#!/usr/bin/ruby# This program gets a specified mammogram from the DDSM website and# converts it to a PNG image. See the help message for full Details.require ' net/ftp ' # Specify the name of the Info-file.def info_file_name ' Info-file. TXT ' enddef image_names ' image_name.txt ' end# Get an FTP file as specified by a DDSM path (e.g.,#/pub/ddsm/cases/cancers/c Ancer_06/case1141/a-1141-1.ics) and return the# Local path to the file, or return nil if the file could isn't be DOWLOADED.D EF get_file_via_ftp (ddsm_path) FTP = net::ftp.new (' figment.csee.usf.edu ') ftp.passive = True Ftp.login ftp.chdir (Fil E.dirname (Ddsm_path)) puts File.basename (Ddsm_path) ftp.getbinaryfile (File.basename (Ddsm_path)) #ftp. Getbinaryfile (Ddsm_path) # would be stored local to this program, under the same file name # Check for Make sure that we manage  D to get the file. if! Filetest.exist? (File.basename (Ddsm_path)) puts "Could not get the File #{file.basename (Ddsm_path)} from the DDSM FTP server; PerhaPS the server is busy. " Exit ( -1) End return File.basename (Ddsm_path) end# return the string input with the system's filesep at the end; If there# is one there already then return Input.def ensure_filesep_terminated (input) if INPUT[INPUT.LENGTH-1].CHR! = Fil E::separator input + = File::separator End return inputend# Check program input;  Input is the program input (i.e ARGV). def check_inputs (input) if input.length! = 1 puts Get_help exit ( -1) END #  See if the user wanted the help docs.  If input[0] = = '--help ' puts Get_help exit ( -1) End # Check to make sure that the info file exists. if! Filetest.exist? (Info_file_name) puts "the file #{info_file_name} does not exist; Use Catalogue-ddsm-ftp-server.rb "Exit ( -1) endend# Given The name of a DDSM image, return the path to the#. ics File A Ssociated with the image name.  If we can ' t find the # path, then we return nil.def get_ics_path_for_image (image_name) # Does image_name look right? If image_name[/. _\d{4,4}_.\.    +/].nil? Raise ' image_name seems to be wrong.  It is: ' + image_name End # Edit The image name, as. ics files has the format ' a-0384-1.ics '; # there is no '.  RIGHT_CC ' (for example). Image_name = image_name[0: ( Image_name.rindex ('. ')  -1)] # Strip everything after and including the last '.  IMAGE_NAME[1] = '-' image_name[6] = '-' # Change the ' _ ' s to '-' s (better regexp-based approach?).  image_name+= '. ICs ' # Add the file suffix.  # Get the path to the. ics file for the specified image.    File.Open (info_file_name) do |file|      File.each_line do |line|      # Does the Specify the. ics file for the specified image name?        If!line[/.+#{image_name}/].nil?  # If so, we can stop looking return line end End END # If we get here and then we do not find a match, so  We'll return nil. Return nilend# Given a line from an. ics file, return a string that specifies the# number of rows and cols in the Image des Cribed by the line. the# string would Be ' 123 456 ' If the image have 123 rows and 456 cols.def get_image_dims (line) rows = line[/.+lines\s\d+/][/\d+/] cols = line[/.+pixels_per_line\s\d+/][/pixels_per_line\s\d+/][/\d+/] Return rows + ' + colsend# Given an image name and a stri Ng representing the location of a# Local. ics file, get the image dimensions and digitizer name for# image_name. Return a hash Which:image_dims maps to a string of the# image dims (which would is ' 123 456 ' If the image has 123 rows an d# 456 cols) and:d Igitizer maps to the digitizer name. If we can ' t# determine the dimensions and/or digitizer name, the corresponding# entry in the hash would be nil.def Get_imag E_dims_and_digitizer (Image_name, Ics_file) # Get The name of the image view (e.g. ' right_cc ') Image_view = Image_name[im Age_name.rindex ('. ')   +1..IMAGE_NAME.LENGTH-1] Image_dims = Nil digitizer = nil # Read the image dimensions and digitizer name from the file.    File.Open (Ics_file, ' R ') do |file|      File.each_line do |line| If!line[/#{image_view}.+/].nil?        # Read the image dimensions Image_dims = get_image_dims (line) End If!line[/digitizer.+/].nil?        # Read The digitizer type from the file.        Digitizer = line.split[1].downcase # Get The second word in the digitizer line.        # There is types of Howtek scanner and they is # Distinguished by the first letter in Image_name. If digitizer = = ' Howtek ' if image_name[0..0].upcase = = ' A ' digitizer + = '-mgh ' elsif image_na Me[0..0].upcase = = ' D ' digitizer + = '-ismd ' else raise ' Error trying to determine Howtek di          Gitizer Variant. '  End End End End # Return an associative array specifying the image dimensions and # digitizer used.  return {: Image_dims = image_dims,:d igitizer =>digitizer}end# Given The name of a DDSM image, return a string that describes# the image dimensions and the name of the digitizer that is used to# CApture it.  If def do_get_image_info (image_name) # Get the path to the ICS file for Image_name.    Ftp_path = Get_ics_path_for_image (image_name) ftp_path.chomp! # Get the ICS file;  Providing us with a string representing # The local location of the file.  Ics_file = Get_file_via_ftp (ftp_path) # Get the image dimensions and digitizer for image_name. Image_dims_and_digitizer = Get_image_dims_and_digitizer (Image_name, Ics_file) # Remove the. ics file as we don ' t need it  any more.  File.delete (ics_file) return image_dims_and_digitizerend# Given a mammogram name and the path to the image info file, get the# image dimensions and digitizer name String.def get_image_info (image_name) # Get the image dimensions and digitizer  Type for the specified # image as a string.  Image_info = Do_get_image_info (image_name) # Now output of the result to standard output. ALL_OK =!image_info[:image_dims].nil? &&!image_info[:d Igitizer].nil?  # is everything OK? If All_ok ret_val = image_iNfo[:image_dims] + ' + image_info[:d Igitizer] End return ret_valend# return a non-existant random filename.def Get_tem P_filename rand_name = "#{rand (10000000)}" # A longish string if filetest.exist?  (rand_name) Rand_name = Get_temp_filename End return rand_nameend# Retrieve the Ljpeg file for the mammogram with the specified# Image_name, given the path to the info file. Return the path to the# local file if successful.  If we can ' t get the file, then return nil.def get_ljpeg (image_name) # Get the path to the image file on the mirror of the  FTP server.    Path = Nil File.Open (info_file_name) do |file|      File.each_line do |line| If!line[/.+#{image_name}\.        Ljpeg/].nil?        # We ' ve found it, so get the file.        line.chomp! Local_path = get_file_via_ftp (line) return Local_path end End # If We get here we didn ' t find where t  The He file is on the server. Return nilend# Given The path to the dir containing the "JPEG program", the path to a# ljpegFile, convert it to a PNM file.  Return the path to the pnm# file.def ljpeg_to_pnm (Ljpeg_file, Dims_and_digitizer) # First convert it to RAW format.  Command = "./jpeg.exe-d-S #{ljpeg_file}" ' #{command} ' # Run it.    Raw_file = Ljpeg_file + '. 1 ' # The JPEG program adds a. 1 suffix.  # See if the. 1 file is created. if! Filetest.exist?  (raw_file) Raise ' Could not convert from Ljpeg to Raw. '  End # Now convert the raw file to PNM and delete the raw file. Command = "./ddsmraw2pnm.exe #{raw_file} #{dims_and_digitizer}" Pnm_file = ' #{command} ' File.delete (raw_file) if $?! =  0 Raise ' Could not convert from raw to PNM. '  End # Return The path to the PNM file. Return pnm_file.split[0]end# Convert A PNM file to a PNG file. Pnm_file are the path to the PNM file# and Target_png_file are the name of the PNG file that we want Created.def pnm_to_png ( Pnm_file, target_png_file) command = "Convert-depth #{pnm_file} #{target_png_file}" ' #{command} ' if! Filetest.exist? (Target_png_fiLe) Raise ' Could not-convert from PNM to PNG. ' End return Target_png_fileend#write image_names to Image_nama.txtdef write_image_names (name) Namefile=file.open (image _names, ' a ') namefile.puts name namefile.puts "\ r \ n" namefile.closeend# the entry point of the program.def main # Ch  Eck to see if the input is sensible.    #check_inputs (ARGV) #image_name = argv[0] File.Open (' Read_names.txt ', ' r ') do |file|  File.each_line do |line|    Image_name = line image_name.chomp!  # Get the image dimensions and digitizer name string for the # specified image. Image_info = Get_image_info (image_name) # Get the Ljpeg file from the mirror of the FTP site, returning the # path T  o The local file.  Ljpeg_file = Get_ljpeg (image_name) # Convert The Ljpeg file to PNM and delete the original ljpeg.  Pnm_file = LJPEG_TO_PNM (Ljpeg_file, Image_info) File.delete (ljpeg_file) # now convert the PNM file to PNG and delete the  PNG file. Target_png_file = Image_name + '. png ' png_file = PNM_to_png (Pnm_file, Target_png_file) File.delete (pnm_file) # Test to see if we got something. if! Filetest.exist?  (png_file) Raise ' Could not create PNG file. ' Exit ( -1) End # Display The path to the file. Puts File.expand_path (png_file) #write image name Write_image_names (image_name) #exit (0) End end exit (0) end# the Help Messagedef get_help <<end_of_help This program gets a specified mammogram from a local mirror of the DDSM FT P Server, converts it to a PNG image and saves it to a target directory; If the target directory already contains a suitably-named file, the download and conversion are skipped. Call this program Using:ruby get-ddsm-mammo.rb <image-name> (note:the ' \ \ ' Simply indicates that the above comma  nd should is on the one line.) Where: * <image-name> is the name of the DDSM image want to get and convert, for example: ' A_1141_1.left_mlo  '. If successful, the program would print the path to the PNG file of the requested mammogram to Standard output and would return a status code of 0. If unsuccessful, the program should display a useful error message and return a Non-zero status code. end_of_helpend# Call the entry Point.main
a very troublesome point is that the original program to run the need to manually enter the image name, only one image at a time, an image processing can be processed after the next one, very time and effort, so in the program posted above I also made a little change, can batch processing images. Method is the name of the image to be processed in advance written in a TXT file, named Read_names, run the program just enter./get-ddsm-mammo. The program run interface is as follows:

After each processing of an image, the program will write the name of the image in a txt file named Image_name, so before you run the program, create a txt file called Image_name.

The last point to note is that the user manual mentions that you want to install Ruby at the same time when installing Cygwin, because at the time the Cygwin version is low, Ruby is not in the manual as shown, but separately, to install as shown in the two:

And









Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

DDSM Database Conversion image format--ljpeg to PNG format

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.