View Single Post
  #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