Go to Post ...and that is exactly why Dave is the coolest nerd I know. - Rich Kressly [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 09-02-2014, 11:10
Justin m's Avatar
Justin m Justin m is offline
Registered User
FRC #2028 (Phantom Mentalists)
Team Role: Programmer
 
Join Date: Nov 2012
Rookie Year: 2012
Location: Hampton, va
Posts: 14
Justin m is an unknown quantity at this point
Vision Code HELP!!!!

Hello,
I am trying to get our vision code to count the pixels in a certain region to see if the goal is hot.
Code:
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2008. All Rights Reserved.                             */
/* Open Source Software - may be modified and shared by FRC teams. The code   */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project.                                                               */
/*----------------------------------------------------------------------------*/
package edu.wpi.first.wpilibj.templates;

import edu.wpi.first.wpilibj.SimpleRobot;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.camera.AxisCamera;
import edu.wpi.first.wpilibj.camera.AxisCameraException;
import edu.wpi.first.wpilibj.image.ColorImage;
import edu.wpi.first.wpilibj.image.NIVisionException;
import edu.wpi.first.wpilibj.image.BinaryImage;
import edu.wpi.first.wpilibj.image.CriteriaCollection;
import edu.wpi.first.wpilibj.image.ParticleAnalysisReport;
import com.sun.squawk.util.MathUtils;

/**
 * The VM is configured to automatically run this class, and to call the
 * functions corresponding to each mode, as described in the SimpleRobot
 * documentation. If you change the name of this class or the package after
 * creating this project, you must also update the manifest file in the resource
 * directory.
 */
public class RobotTemplate extends SimpleRobot {

    AxisCamera camera;
    public ColorImage image;
    public BinaryImage threshold;
    private int imageState;
    private boolean busy;
    public static final int left = 402;
    public static final int right = 456;
    public static final int top = 75;
    public static final int bottom = 99;
    public int countHotPixels = 0;
    public int count = 0;

    public RobotTemplate() {
        camera = AxisCamera.getInstance("10.20.28.11");
        camera.writeResolution(AxisCamera.ResolutionT.k640x480);
        camera.writeExposurePriority(AxisCamera.ExposurePriorityT.imageQuality);
        camera.writeExposureControl(AxisCamera.ExposureT.hold);
        camera.writeWhiteBalance(AxisCamera.WhiteBalanceT.fixedIndoor);
    }

    public void autonomous() {
    }

    /**
     * This function is called once each time the robot enters operator control.
     */
    public void operatorControl() {
        while (isEnabled() && isOperatorControl()) {
            try {
                switch (imageState) {
                    case 0:
                        busy = true;
                        image = camera.getImage();
//                        System.out.println("Got image");
                        imageState++;
                        busy = false;
                        break;
                    case 1:
                        busy = true;
                        threshold = image.thresholdRGB(0, 70, 185, 255, 145, 225);  // green values
//                        thresholdImage = image.thresholdHSV(115, 125, 195, 255, 220, 255);
                        image.free();
                        image = null;
//                        System.out.println("Got threshold");
                        imageState++;
                        busy = false;
                        break;
                    case 2:
                        busy = true;
                        for(int x = left; x <= right; x++)
                        {
                            for(int y = top; y <= bottom; y++)
                            {
                                byte pixel = threshold.image.getByte(x);
                                System.out.println("\tChecking (" + x + ", " + y + ": " + pixel);
                                if(pixel != 0)
                                {
                                    countHotPixels++;
                                    count++;
                                    break;
                                }
                            }
                        }
                        imageState++;
                        busy = false;
                        threshold.free();
                        threshold = null;
                        break;
                    case 3:
                        System.out.println("Number hot pixels : " + countHotPixels);
                        busy = false;
                        imageState = 0;
                        break;
                }
                
            } catch (AxisCameraException ex) {
                ex.printStackTrace();
            } catch (NIVisionException ex) {
                ex.printStackTrace();
            }
            Timer.delay(0.1);
            getWatchdog().feed();
        }

    }

    /**
     * This function is called once each time the robot enters test mode.
     */
    public void test() {
    }
}
Every time I run this, I either get a NoImageAvailable exception on the image = camera.getImage() or I get a OffsetOutOfBounds exception on the byte pixel = threshold.image.getByte(x); I apologize for the poor formatting of this message. Any suggestions would be greatly appreciated
  #2   Spotlight this post!  
Unread 09-02-2014, 11:16
Thorn Thorn is offline
Registered User
None #2869 (Regal Eagles)
Team Role: Teacher
 
Join Date: Jan 2014
Rookie Year: 2012
Location: Bethpage
Posts: 10
Thorn is an unknown quantity at this point
Re: Vision Code HELP!!!!

You should just try to capture one image (not in a while loop) and verify that it is there using FTP. Every time you capture an image it gets saved to the cRIO Flash, so you will run out of memory quickly if you continue to capture images within your while loop (unless you deleted it and I didn't see that in your code).

My guess is that your exception is coming from not being able to store the image because you are out of memory (from previous runs of the program) or something else unrelated to your code. FTP into the device to see if there are any images saved.

Try testing the vision code that was handed to us with the latest Netbeans plugin. There is a sample program included (you can get to it by creating a new project and choosing samples)
  #3   Spotlight this post!  
Unread 09-02-2014, 11:25
Justin m's Avatar
Justin m Justin m is offline
Registered User
FRC #2028 (Phantom Mentalists)
Team Role: Programmer
 
Join Date: Nov 2012
Rookie Year: 2012
Location: Hampton, va
Posts: 14
Justin m is an unknown quantity at this point
Re: Vision Code HELP!!!!

The robot only tries to get an image the first time through the loop due to the switch statement. Once it has an image, it then processes it based on color and then it goes to the for loop, with each action one pass through the loop.
  #4   Spotlight this post!  
Unread 15-02-2014, 03:18
sailorjoe sailorjoe is offline
Mentor, RoboEagles, FWHS
AKA: Joe Hafner
FRC #4579 (RoboEagles)
Team Role: Mentor
 
Join Date: Jan 2014
Rookie Year: 2014
Location: Auburn, WA
Posts: 10
sailorjoe is an unknown quantity at this point
Re: Vision Code HELP!!!!

Did you get his figured out?
If not, take a look at two classes in javadocs. BinaryImage and ParticleAnalysisReport. You can save yourself a lot of work with these.
Good luck!
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 02:46.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi