Extract foreground from binary image

hi
i need help in java programming in java.
i have a binary image
by the code
BufferedImage image = ImageIO.read(new File("f:/123.JPG"));
BufferedImage bwImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
//bwImage.createGraphics().drawImage(image, 0, 0, null);
BufferedImage bwImage1 = new BufferedImage(bwImage.getWidth(), bwImage.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
//bwImage1.createGraphics().drawImage(image, 0, 0, null);
i need only foreground in color..can u give me the code for that..its urgent..

If you want any color whatsoever, then you need to use a BufferedImage type that supports color. BufferedImage.TYPE_BYTE_BINARY wont do. In the example below I use BufferedImage.TYPE_INT_RGB. The foreground remains in color, while everthing else turns white.
BufferedImage image = ImageIO.read(...);
        BufferedImage fgImage =                     //foreground image
                new BufferedImage(image.getWidth(),image.getHeight(),
                BufferedImage.TYPE_INT_RGB);
        for(int x = 0; x < fgImage.getWidth(); x++) {
            for(int y = 0; y < fgImage.getHeight(); y++) {
                int rgbSrc = image.getRGB(x, y);
                if(rgbSrc == rgbForeground) {
                    fgImage.setRGB(x,y,rgbForeground); //remain in color
                }else{
                    fgImage.setRGB(x,y,-1);  //set white
        }Or are you looking for more of a Sin City effect where one thing remains in color and everything else is in grayscale?
BufferedImage image = ImageIO.read(...);
        BufferedImage fgImage =                     //foreground image
                new BufferedImage(image.getWidth(),image.getHeight(),
                BufferedImage.TYPE_INT_RGB);
        for(int x = 0; x < fgImage.getWidth(); x++) {
            for(int y = 0; y < fgImage.getHeight(); y++) {
                int rgbSrc = image.getRGB(x, y);
                if(rgbSrc == rgbForeground) {
                    fgImage.setRGB(x,y,rgbForeground); //remain in color
                }else{
                    int r = (rgbSrc>>16)&0xff;
                    int g = (rgbSrc>> 8)&0xff;
                    int b = (rgbSrc    )&0xff;
                    int lum = (int) (.299 * r + .587 * g + .144 * b);
                    //set to grayscale
                    fgImage.setRGB(x,y,(255<<24)|(lum<<16)|(lum<<8)|lum);
        }

Similar Messages

  • Extracting strings from binary data

    Hello,
    I am trying to extract string from a binary file.
    At the unix command line (sunos) I can just type;
    strings <filename>
    This is a nice way to get a list of the contents of a directory.
    Is there a way in pl/sql to extract strings from binary data ? An equiv to strings on unix/linux ?
    Thanks in advance.
    Ben

    Hi,
    If you do want to list the contents of a directory, there are other ways to do it. Here's a base implementation of a utility I wrote:
    create or replace and resolve java source named "Util" as
    import java.io.*;
    import java.sql.*;
    import oracle.sql.*;
    import oracle.jdbc.driver.*;
    public class Util {
        public static void listFiles(String directory, oracle.sql.ARRAY[] names)
            throws IOException, SQLException {
            File f = new File(directory);
            if(f==null)
                throw new IOException("Directory: "+directory+" does not exist.");
            String[] files = f.list(
                new FilenameFilter() {
                    public boolean accept(File dir, String name) {
                        // List all files
                        return true;
            Connection conn = new OracleDriver().defaultConnection();
            ArrayDescriptor descriptor = ArrayDescriptor.createDescriptor("VC_TAB_TYPE", conn);
            names[0] = new ARRAY(descriptor, conn, files);
            return;
    create or replace type vc_tab_type is table of varchar2(255);
    create or replace package util authid current_user as
        function list_files(p_directory in varchar2)
            return vc_tab_type;
    end;
    create or replace package body util as
        procedure list_files (
            p_directory in varchar2
            , p_filenames out nocopy vc_tab_type
        is
        language java
        name 'Util.listFiles(java.lang.String, oracle.sql.ARRAY[])';
        function list_files(p_directory in varchar2) return vc_tab_type
        is
            l_filenames vc_tab_type := vc_tab_type();
        begin
            list_files(p_directory, l_filenames);
            return l_filenames;
        end;
    end;
    /You can then query the filesystem as follows:
      1  select column_value as filename
      2  from table(util.list_files('c:\windows'))
      3  where column_value like '%.log'
      4* and rownum <= 10
    SQL> /
    FILENAME
    0.log
    AdfsOcm.log
    aspnetocm.log
    bkupinst.log
    certocm.log
    chipset.log
    cmsetacl.log
    comsetup.log
    DtcInstall.log
    FaxSetup.log
    10 rows selected.cheers,
    Anthony

  • Retain color to foreground in binary image

    hi
    i need some help in the java.
    I have binary image whose foreground is white nad background is black by the following code
    BufferedImage image = ImageIO.read(new File("f:/123.JPG"));
    BufferedImage bwImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
    BufferedImage fgImage = //foreground image
    new BufferedImage(bwImage.getWidth(),bwImage.getHeight(),
    BufferedImage.TYPE_BINARY_IMAGE);
    now i want to retain color to the foreground only..
    can anyone help me..pleaseeeeeee

    Here is my idea to display the pic on the webpage:
    The pic is stored in DB as binary, after retrieving it, the binary data will be converted to JPEG pic. Then, the pic will be saved to the local machine automaticly. After that, I get the pic's SRC, and use html display it.
    My code is as follow (not completed), some errors in it.     public static String getPayoffSRC(Services serv, long fileId)
                                                                    throws FileNotFoundException,
                                                                  DBException,
                                                                  DbFileNotFoundException,
                                                              IOException {
            BufferedImage buffy = new BufferedImage(300, 400,
                                                        java.awt.image.BufferedImage.TYPE_INT_RGB);   
            FileOutputStream out = new FileOutputStream(serv.getFileManager().getFileDb(fileId).getFileName());
            JPEGImageEncoder jencoder = JPEGCodec.createJPEGEncoder(out);
            JPEGEncodeParam enParam = jencoder.getDefaultJPEGEncodeParam(buffy);
            enParam.setQuality(1.0F, true);
            jencoder.setJPEGEncodeParam(enParam);
            jencoder.encode(buffy);
            out.close();
            return Pic's SRC,
        }

  • Extracting data from an image (Java Steganography)

    Hey all!
    I am writing an application in Java that hides bytes in an images pixel's Least Significant Bits.
    Here is the embeding function (mind you, its at an early stage). As far as I can tell, it works perfectly...
         public BufferedImage LSB_BufferedImageEmbed(BufferedImage image, byte[] data)
              int imgWidth = image.getWidth(null);
              int imgHeight = image.getHeight(null);
              //preformat the data array and return as an array of ints?
              int[] formated_data = FormatDataArray(data);
              int index = 0;
              try
                   for(int i = 0; i < imgHeight; i++)
                        for(int j = 0; j < imgWidth; j++)
                             if(index == formated_data.length)
                                  break;
                             System.out.print("Pixel prior Embed: " + image.getRGB(j, i) + "\n");
                             int current_pixel = image.getRGB(j, i);
                             int current_data = formated_data[index++];
                             //Perform bitwise AND on current pixel value - isolating the Least Significant bits.
                             //NB: Mask is = ff fc fc fc [Alpha Byte] [Red] [Green] [Blue]
                             //1111 1111 - 1111 1100 - 1111 1100 - 1111 1100
                             current_pixel &= 0xfffcfcfc;
                             //x << y is "shift x right by y spaces"
                             //Add bits 5,4 of current data to the bottom of the red channel - xx -- xx xx
                             current_pixel = current_pixel + ((current_data & 0x30) << 12);
                             //Add bits 3,2 of current data to the bottom of the green channel - xx xx -- xx
                             current_pixel = current_pixel + ((current_data & 0x0c) << 6);
                             //Add bits 1,0 of current data to the bottom of the blue channel - xx xx xx --
                             current_pixel = current_pixel + (current_data & 0x03);
                             image.setRGB(j, i, current_pixel);
              catch(Exception e)
                   System.out.println("Problem with LSB!!");
              return image;
    And here's the corresponding extraction method
         public byte[] LSB_ExtractFromBufferedImage(BufferedImage image)
              int imgWidth = image.getWidth(null);
              int imgHeight = image.getHeight(null);
              int index = 0;
              byte[] extracted_bytes;
              byte[] extracted_data = new byte[12];
              try
                   for (int i = 0; i < imgHeight; i++)
                        for (int j = 0; j < imgWidth; j++)
                             if(index == extracted_data.length)
                                  break;
                             System.out.print("Pixel prior Extract: " + image.getRGB(j, i) + "\n");
                             int current_pixel = image.getRGB(j, i);
                             //Isolate the bit pattern needed for the Int to byte[] method...
                             current_pixel &= 0x00030303;
                             extracted_bytes = IntToByteArray(current_pixel);
                             for(int t = 0; t < 4; t++)
                                  extracted_data[index++] = extracted_bytes[t];
              catch(Exception e)
                   System.out.println("Problem with LSB Extraction!!");
              return extracted_data;
    Unfortunately, this doesn't work all that well (by that, I mean not working at all!) It does return the bits alright, but not the ones that were previosuly embedded. Does anyone have any ideas on any aspect of these methods as to why that shouldn't work??
    Any help is greatly appreciated!
    Cougaris

    Are you sure that you interpret your results correctly ?
    Your routines use different data representations and that leads easily to misinterpretation.
    Especially as you only use 6 Bits from an int but take 4 Bytes as result.
    Post some input and output data. i.e. an array of 3 values for formattedData and the 12 resulting bytes. I expect the data to be correct (relative to your code, probably not to your intentions).
    Regards
    SH

  • Can't Extract Files from System Image DVD

    I recently created a Windows 7 system image onto DVD-RWs as my HDD was suspect.  The HDD was replaced under warranty. When I came to restore the system image I got up to the fourth DVD and the process would not go any further (could not read the DVD).
    I then tried to extract some of the more important files from the first three disks that the restore process had read successfully.  I used the AttachVHD option under Computer management however for every .vhd file which I tried to attach I received a "The file or directory is corrupted anf unreadable" error message from the Virtual Disk Manager.
    I also tried 7-Zip to extract files but received the message "Can not open file as archive" each time.
    Can any one throw any light on why I can't read these .vfd files?

    marckelli
    Only last night a user went through the same situation as you are going through. Details are important. For now I am assuming that your computer is Windows 7, 8, or 8.1 64 bit. If other post immediately to let me know.
    1. You need two files from Adobe, one .exe and the other .7z. If you have a 64 bit computer operating system
    make sure the you see 64 in the name of the file.
    2. Assure that you have antivirus and firewall disabled.
    3. Right click the .exe file and select Run As Administrator. The double click the .exe to Run it. This will lead
    you into the 7z which is the program.
    If the desktop is not working as the destination for the Adobe Premiere Elements 12 Folder which will be present after the 7z has completed, then set it to Documents.
    In the Adobe Premiere Elements 12 Folder, double click the PRE12 Folder to find the Setup.exe from which you will
    install the program.
    I will be back in a moment to add the link to last night's thread where we went through what you are going through now.
    Make sure at the onset that you have the right files to be used with your specific operating system.
    ATR
    Add On...This is last night's thread on this type of issue
    Premeire elements 12 will only download as files...

  • Extract drivers from boot image

    I have a boot image and my team members added lot of drivers on top of boot images.... Now I want to get a report to get the list of drivers that are integrated
    and their source path as I must  have to keep them in a separate folder so that in my up coming migration project I can use them again to integrate in new boot images when migrated from sccm 2012 RTM to SP1 and R2
    Kindly through some light on this
    HunBoy Started learning from this page

    You should not use the drivers from your old boot images in your new boot images.
    Windows PE drivers are not the same for every verison of ConfigMgr.
    Win PE 5.0 = SCCM 2012 R2
    Win PE 4.0 = SCCM 2012 SP1
    Win PE 3.0 = SCCM 2012 RTM
    And the drivers you need are:
    Win PE 5.0 = Windows 8.1
    Win PE 4.0 = Windows 8
    Win PE 3.0 = Windows 7
    Windows PE 5.0 also includes many new drivers so drivers that you needed in older versions of PE might not be required any more...
    Ronni Pedersen | Microsoft MVP - ConfigMgr | Blogs:
    www.ronnipedersen.com/ and www.SCUG.dk/ | Twitter
    @ronnipedersen

  • Extract text from TIFF image

    If I have an image of a scanned document (TIFF) is it possible using Oracle text to index and subsequently search on text elements of that image?
    If not, anyone have ideas or suggestions of how to accomplish this goal?
    TIA

    No - you would need to feed the TIF document through an OCR program to identify the text first. It might be possible to implement an OCR program as a user filter if it provided a suitable API.
    Oracle Text itself does not include OCR functionality.
    - Roger Ford.

  • How to extract the license plate region from the image of cars

    HI, I want to extract the license plate region from the image of cars with vision assistant. I try to adjust the threshold value, remove small objects. But i still cannot get the perfect license plate region. I have attached 4 images that i am working now. I hope someone can help me to extract that region. Really thanks.
    Attachments:
    IMG_2029.JPG ‏150 KB
    IMG_2031.JPG ‏155 KB
    IMG_2089.JPG ‏130 KB

    Hi, I have attached my extrated images.. Please check them...
    Attachments:
    35.PNG ‏17 KB
    36.PNG ‏12 KB
    37.PNG ‏13 KB

  • How to render image from binary property of node?

    Hello!
    How to put the image to the page from binary property of node in cms? How tags can i use?

    Hi,
    I have an additional question, regarding the images of a page. As stated above, rendering them works fine. But now I would also like to have them resized within certain boundaries. Is there a way to define the min/max width/height in the design of the page component for these images?
    I tried adding a node (name='image', jcr:primaryType=nt:unstructured) to /etc/designs/<myproject>/jcr:content/<mypagecomponent>. I then added minHeight, minWidth, maxHeight, ... properties to that node, but it seems they are not being picked up by the img.png.java servlet from the libs/foundation/page component.
    I created this design node based on a similar node which was added in the design of <mypagecomponent> for an 'image' in a 'par' when I changed the design of it.
    So I'm wondering if it is at all possible to define a design for the image of a page and if it, how I should do it.
    Kind regards,
    Luc

  • 3502i image extraction failure from WLC

    This is a basic CAPWAP connection with static address on AP to WLC.  Fails on extraction of image.   Here is the console output from the AP:
    examining image...!
    extracting info (292 bytes)
    Image info:
        Version Suffix: k9w8-.124-23c.JA2
        Image Name: ap3g1-k9w8-mx.124-23c.JA2
        Version Directory: ap3g1-k9w8-mx.124-23c.JA2
        Ios Image Size: 5612032
        Total Image Size: 5919232
        Image Feature: WIRELESS LAN|LWAPP
        Image Family: AP3G1
        Wireless Switch Management Version: 7.0.116.0
    Extracting files...
    ap3g1-k9w8-mx.124-23c.JA2/ (directory) 0 (bytes)
    extracting ap3g1-k9w8-mx.124-23c.JA2/Z5.bin (29492 bytes)!!
    extracting ap3g1-k9w8-mx.124-23c.JA2/ap3g1-k9w8-mx.124-23c.JA2 (4721941 bytes)!!!!!
    *Jun 21 18:29:31.000: %CAPWAP-5-DTLSREQSEND: DTLS connection request sent peer_ip: x.x.31.61 peer_port: 5246
    *Jun 21 18:29:31.000: %CAPWAP-5-CHANGED: CAPWAP changed state to
    *Jun 21 18:29:31.569: %CAPWAP-5-DTLSREQSUCC: DTLS connection created sucessfully peer_ip: x.x.31.61 peer_port: 5246
    *Jun 21 18:29:31.572: %CAPWAP-5-SENDJOIN: sending Join Request to x.x.31.61
    *Jun 21 18:29:31.572: %CAPWAP-5-CHANGED: CAPWAP changed state to JOINperform archive download capwap:/ap3g1 tar file
    *Jun 21 18:29:31.578: %CAPWAP-5-AP_IMG_DWNLD: Required image not found on AP. Downloading image from Controller.
    *Jun 21 18:29:31.581: %CAPWAP-5-CHANGED: CAPWAP changed state to IMAGE
    *Jun 21 18:29:31.581: Loading file /ap3g1...
    Premature end of tar file
    ERROR: Problem extracting files from archive.
    archive download: takes 80 seconds
    *Jun 21 18:30:49.633: %DTLS-5-SEND_ALERT: Send FATAL : Close notify Alert to x.x.31.61:5246
    *Jun 21 18:30:49.636: %CAPWAP-5-CHANGED: CAPWAP changed state to DISCOVERY
    *Jun 21 18:30:49.636: %CAPWAP-5-CHANGED: CAPWAP changed state to DISCOVERY
    *Jun 21 18:30:49.690: %CAPWAP-3-ERRORLOG: capwap ifs:  read error or timeout
    *Jun 21 18:30:51.521: capwap_image_proc: problem extracting tar file
    It looks to me like the image might be corrupt on the WLC, but I can't figure out how to update it on the WLC.   I have had this equipment for two weeks, so I am still figuring it out.   
    Any suggestions or guidance would be appreciated.
    Thanks,
    Noah

    Thank you this helped me solved an issue I had with a 1042. Rebooting the WLC solved the issue following the upgrade of our WISMs from version 7.0.116.0 to version 7.0.220.0

  • Extract contour binary image

    Hi, I've been trying to use the IMAQ Extract Contour VI to get a contour of a seeded jet (is a binary image). t is sent to the IMAQ Extract Contour VI, but this last one fails to find the contour. I don't know if the algorithm is able for this kind of problem.
    Could someone help me please? The Vi and the image are attached below. 
    Attachments:
    3.png ‏554 KB
    extract contour.vi ‏46 KB

    Hi Giansorr,
    thank you for your post on NI forum. In order to perform a Contour Analysis, you can try to analyze some examples you can find at the following path:
    <LabVIEW>\examples\Vision\2. Functions\Contour Analysis
    The contour algorithm works fine if the image you are going to analyze has wel defined contours.
    You can try to run the above examples with your .png image.
    Have a nice work.
    Claudio Cupini
    NI ITALY
    Applications Engineering Dept.

  • How to extract the gray from an image

    Hi.
    In Photofiltre, it's dead easy to extract the gray tones from an image to boost up the colours. It's often better than just increasing saturation or so.
    But since I have Photoshop CS3 I'd like to do it there. It must be possible to do something like that too. But how?? I just can't find the right way?
    Anybody here knows the answer?
    LSc

    Select --> Color Range or do some magic with Calculations to create a selection based on luminance.
    Mylenium

  • [CS2, JS] Extracting Metadata from Images

    I have to place about 100 images into a document and label them with metadata from the images (Description and author)
    I'm happy just for the data to be pulled from the image and placed in a text frame over the image - i can reposition it later.
    any suggestions on how to go about this - or where to start on a script to do it?

    Look at Olav's script called LabelGraphics.jsx
    Loic

  • MVC Display Binary Image from ViewBag

    I am having problem when displaying the binary image in MVC view. I stored images in database as binary format and then assigned to ViewBag in Controller. How do I assign viewbag data to image in View?
    Controller
    ItemBO mibo = new ItemBO();
    ViewBag.Picture1 = Convert.ToByte(mibo.GetImage(1));
    View
    var elem = document.createElement("img");
    elem.setAttribute("src", @ViewBag.Picture1);
    elem.setAttribute("style", "height:100%");
    elem.setAttribute("alt", "Image");
    document.getElementById("mydiv").appendChild(elem);
    Above code doesn't work as I expected. Any help would be appreciated.

    MVC section is at the below link.
    http://forums.asp.net/

  • Is there a way to extract iMessages from Mac to PDF or .txt format?

    I've been searching the net but to no avail. All results point to backing up/extracting iMessages from iDevices other than a Mac. I'm going to change my Apple ID and I want to at least have a copy of the previous convos I had if PDF or .txt format. Thanks in advance! 

    HI,
    If you have the Save on Close  option enabled in Messages > Preferences > Messages then all Chats and conversations are saved.
    They can be recalled any time.
    1) In the Messages app the File Menu > Recent Items will list the last X number of chats.
    X is the number set in System Preferences > General pane about the number of Documents, Applications and Servers shown in such lists.
    2) old migrated iChat Saved chats are in Your Home Folder/Documents/iChats
    3) New Saves are in your Home Folder/Library/Messages/Archive although this Library is Hidden.
    In th Finder > Go Menu > Go to Folder and type ~/Library/Messages into the dialogue box that appears.
    I have 10 non iMessages accounts as well as  my iMessages account logged in when I am on line.
    I can call up any of the last Chats shown in Recent Items no matter which account they came from  (different Screen Name or Jabber IDs or even Bonjour chats)
    I have not actually changed my iMessages ID to check but the app will continue to save to the same Folder and it should be able to retrieve recent ones.
    Visiting the folder and double clicking an older chat tends to launch it in a new window separate from the Messages Window itself (no Side bar no, Text field at the bottom, no "To" spot).
    8:31 PM      Friday; March 29, 2013
      iMac 2.5Ghz 5i 2011 (Mountain Lion 10.8.3)
     G4/1GhzDual MDD (Leopard 10.5.8)
     MacBookPro 2Gb (Snow Leopard 10.6.8)
     Mac OS X (10.6.8),
     Couple of iPhones and an iPad
    "Limit the Logs to the Bits above Binary Images."  No, Seriously

Maybe you are looking for